forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
32 lines (26 loc) · 855 Bytes
/
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
const http = require('http');
const mongoose = require('mongoose');
const { logger } = require('./middleware/logger');
const app = require('./app');
const PORT = process.env.PORT || 6673;
const SERVER = http.createServer(app.callback());
// Gracefully close Mongo connection
const gracefulShutdown = () => {
mongoose.connection.close(false, () => {
logger.info('Mongo closed');
SERVER.close(() => {
logger.info('Shutting down...');
process.exit();
});
});
};
// Server start
SERVER.listen(PORT, '0.0.0.0', () => {
logger.info(`Running on port: ${PORT}`);
// Handle kill commands
process.on('SIGTERM', gracefulShutdown);
// Prevent dirty exit on code-fault crashes:
process.on('uncaughtException', gracefulShutdown);
// Prevent promise rejection exits
process.on('unhandledRejection', gracefulShutdown);
});