-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (41 loc) · 1.33 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
49
50
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const app = require('./app');
process.on('uncaughtException', (err) => {
console.log('Uncaught Exception. Shutting Down');
console.log(err.toString());
process.exit(1); //uncaught exception
});
dotenv.config({ path: '.env' }); //load environment variables
//initialize the database using the environment variables.
const DB = process.env.DATABASE.replace('<PASSWORD>', process.env.DB_PASSWORD);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(() => {
console.log('DB Connection successful!');
});
//START THE SERVER
const port = process.env.PORT || 8000; //read port from environment if available else use defined port
const server = app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
//listening for a unhandledRejection event globally
process.on('unhandledRejection', (err) => {
console.log('Unhandled REJECTION. Shutting Down');
console.log(err.toString());
//close the server gracefully
server.close(() => {
process.exit(1); //uncaught exception
});
});
process.on('SIGTERM', () => {
console.log('SIGTERM RECEIVED. Shutting down gracefully');
server.close(() => {
console.log('Process Terminated!');
});
});