-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (48 loc) · 1.51 KB
/
server.js
File metadata and controls
53 lines (48 loc) · 1.51 KB
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
const dotenv = require('dotenv');
const mongoose = require('mongoose');
// ErrorHandling: catching uncaught_exception(programming errors) before starting server
process.on('uncaughtException' , err => {
console.log('UNCAUGHT_EXCEPTION: Shutting down the application...');
console.error({
status: '💥 ERROR 💥',
timeStamp: new Date().toISOString(),
errorDetails: {
reason: err.name,
message: err.message
}
});
process.exit(1);
});
dotenv.config({ path: './.env' });
const app = require('./app');
// Connecting to MongoDB
const connectionString = process.env.DB_URL.replace('<username>', process.env.DB_USERNAME).replace('<password>', process.env.DB_PASSWORD);
// Creating connection
mongoose.connect(connectionString, {
useNewUrlParser : true ,
useCreateIndex : true ,
useFindAndModify : false,
useUnifiedTopology: true
}).then(() =>{
console.log('DB Connected!');
});
// Starting Server
const port = process.env.PORT;
const server = app.listen(port, () => {
console.log(`App server running on port ${port}!`);
});
// ErrorHandling: Outside errors of Node/Express, Unhandled Rejections
process.on('unhandledRejection', err => {
console.log('UNHANDLED_REJECTION: Shutting down the server...');
console.error({
status: '💥 ERROR 💥',
timeStamp: new Date().toISOString(),
errorDetails: {
reason: err.name,
message: err.message
}
});
server.close(() => { //server.close() first closes the server then exists the process
process.exit(1);
});
});