-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (39 loc) · 1.49 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
45
46
47
48
const dotenv = require('dotenv');
const mongoose = require('mongoose');
dotenv.config({ path: './config.env' });
//this error for if you something used not define
//always use top of the all code
process.on('uncaughtException', err => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1); //only process because isn't async()
});
const app = require('./app');
const uri = process.env.DATABASE_URL;
mongoose
.connect(uri, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
})
.then(() => console.log('DB is connected 🚀'));
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
process.on('unhandledRejection', err => {
console.log('UNHANDLED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
//if i'm use directly process.exit it stop server immediately means it not good but we stop server gracefully so first close all request then close server
server.close(() => {
process.exit(1); //it shutdown process 0:success 1:uncalled exception
});
});
//each time some where in application unhandled Rejection also promise rejection occur then the process object emit new () is unhandledRejection ;
process.on('SIGTERM', () => {
console.log('👋 SIGTERM RECEIVED. Shutting down gracefully');
server.close(() => {
console.log('💥 Process terminated!');
});
});