-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (39 loc) · 1.16 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require('express');
const morgan = require('morgan');
const rfs = require('rotating-file-stream');
const path = require('path');
const config = require('config');
const cors = require('cors');
const logger = require('./lib/services/logger');
const boom = require('./lib/services/boom');
const controller = require('./lib/controllers');
const env = process.env.NODE_ENV || 'development';
const app = express();
app.use(cors({
origin: '*',
methods: ['GET', 'POST'],
headers: ['Content-Type'],
maxAge: 10 * 60,
}));
if (env === 'development') {
app.use(morgan('dev'));
} else {
const stream = rfs.createStream('access.log', {
interval: config.get('logRotation'),
path: path.resolve(process.cwd(), 'log'),
});
app.use(morgan('combined', { stream }));
}
app.use(boom());
app.use('/', controller);
const server = app.listen(config.get('port'));
server.setTimeout(1000 * 60 * 30);
logger.info(`Server listening on http://localhost:${config.get('port')}`);
const closeApp = async () => {
logger.info('Closing the server');
server.close(() => {
process.exit(0);
});
};
process.on('SIGINT', closeApp);
process.on('SIGTERM', closeApp);