-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
57 lines (46 loc) · 1.3 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
53
54
55
56
57
'use strict';
import dotenv from "dotenv";
import mongoose from "mongoose";
import logger from "./config/logger.js";
import app from "./app.js";
dotenv.config();
const CONNECTION_URL = process.env.CONNECTION_URL;
const PORT = process.env.PORT || 3000;
// Exit on error
mongoose.connection.on('error', (err) => {
logger.error(`MongoDB connection error: ${err}`);
process.exit(1);
});
// Mongoose logs in development environment
if (process.env.NODE_ENV !== 'production') {
mongoose.set('debug', true);
}
let server;
mongoose.connect(CONNECTION_URL,{useNewUrlParser: true,useUnifiedTopology: true}).then(()=>{
logger.info('Connected to MongoDB');
server = app.listen(PORT, () => {
logger.info(`Listening to port ${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();
// }
// });