forked from ohmytrash/rest-api-nodejs-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) · 999 Bytes
/
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
const http = require('http')
const mongoose = require('mongoose')
const config = require('./config/config')
const logger = require('./config/logger')
const app = require('./app')
const socket = require('./socket')
let server
mongoose.connect(config.mongoose.url, config.mongoose.options).then(() => {
logger.info('Connected to MongoDB')
server = http.createServer(app)
socket(server)
server.listen(config.port, () => {
logger.info(`Listening to port ${config.port} : http://localhost:${config.port}`)
})
})
const exitHandler = () => {
if (server) {
server.close(() => {
logger.info('Server closed')
process.exit(1)
})
} else {
process.exit(1)
}
}
const unexpectedErrorHandler = (error) => {
logger.error(error)
exitHandler()
}
process.on('uncaughtException', unexpectedErrorHandler)
process.on('unhandledRejection', unexpectedErrorHandler)
process.on('SIGTERM', () => {
logger.info('SIGTERM received')
if (server) {
server.close()
}
})