-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (40 loc) · 1.41 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
//dependencies
const mongoose = require('mongoose');
const dotenv = require('dotenv');
//HANDLING UNCAUGHT EXCEPTIONS
process.on('uncaughtException', err => {
console.log('UNCAUGHT EXCEPTION! Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
//reading the env variables from our config.env file and saving them into Node environment variables
dotenv.config({ path: './config.env' });
//first, creating the complete mongodb connection string, with the password for the database
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
//connecting to MongoDB database
mongoose.connect(DB).then(() => console.log('DB Connection successful'));
const app = require('./app');
//establishing the port
const port = process.env.PORT || 3000;
//CREATING SERVER
const server = app.listen(port, () => {
console.log(`App running on port ${port}`);
});
//HANDLING UNHANDLED REJECTIONS
process.on('unhandledRejection', err => {
console.log(err.name, err.message);
console.log('UNHANDLED REJECTION! Shutting down...'); //gracefully shutdown when unhandled rejections
server.close(() => {
process.exit(1);
});
});
//HANDLING SIGTERM SIGNALS FROM HEROKU
process.on('SIGTERM', () => {
console.log('SIGTERM RECEIVED. Shutting down gracefully...');
server.close(() => {
console.log('Process terminated!');
}); //close our server still handling pending requests
});