-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
52 lines (43 loc) · 1.22 KB
/
index.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
50
51
52
'use strict'
// require new relic at the top only in production environment
if (process.env.NODE_ENV === 'production') {
require('newrelic')
}
const config = require('config')
const server = require('./server')
const logger = require('./server/utils/logger')
const gracefulStopServer = function () {
// Wait 10 secs for existing connection to close and then exit.
server.stop({timeout: 10 * 1000}, () => {
logger.info('Shutting down server')
process.exit(0)
})
}
process.on('uncaughtException', err => {
logger.error(err, 'Uncaught exception')
process.exit(1)
})
process.on('unhandledRejection', (reason, promise) => {
logger.error({
promise: promise,
reason: reason
}, 'unhandledRejection')
process.exit(1)
})
process.on('SIGINT', gracefulStopServer)
process.on('SIGTERM', gracefulStopServer)
/**
* Starts the server
* @returns {Promise.<void>}
*/
const startServer = async function () {
try {
// add things here before the app starts, like database connection check etc
await server.start()
logger.info(`server started at port: ${config.get('app.port')} with env: ${config.util.getEnv('NODE_ENV')}`)
} catch (error) {
logger.error(error)
process.exit(1)
}
}
startServer()