-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (33 loc) · 1.06 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
'use strict'
const log = require('pino')({ level: 'info' })
const server = require('./app')({ logger: log, trustProxy: true })
const port = process.env.PORT || '3000'
const host = process.env.HOST || 'localhost'
const options = { port, host }
// --- Server Listen ---
server.listen(options, function (err, address) {
if (err) {
server.log.error(err)
process.exit(1)
}
server.log.info(`Server listening on ${address}`)
})
// --- Graceful Shutdown ---
async function closeGracefully (signal) {
log.warn(`Received signal to terminate: ${signal}`)
await server.close()
// await db.close() if we have a db connection in this app
// await other things we should cleanup nicely
process.exit()
}
process.on('SIGINT', closeGracefully)
process.on('SIGTERM', closeGracefully)
// --- Handle Errors ---
process.on('uncaughtException', (err) => {
log.error(`Uncaught Exception: ${err.message}`)
process.exit(1)
})
process.on('unhandledRejection', (reason, promise) => {
log.error('Unhandled rejection at ', promise, `reason: ${reason}`)
process.exit(1)
})