-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
47 lines (40 loc) · 1.13 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
const mongoose = require('mongoose');
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });
const app = require('./app');
// Handling Uncaught Exceptions
process.on('uncaughtException', (err) => {
// eslint-disable-next-line no-console
console.log(`Uncaught Exception: ${err}`);
process.exit(1);
});
const port = process.env.PORT || 3000;
// Add the password to connection string
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
// Connect to DB
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
// eslint-disable-next-line no-console
console.log('Database Connection Successful');
});
const server = app.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`App Running on port ${port} ...`);
});
// Handling Promise Unhandled Rejection
process.on('unhandledRejection', (err) => {
// eslint-disable-next-line no-console
console.log(`Unhandled Rejection: ${err}`);
// Gracefully shutdown server and app
server.close(() => {
process.exit(1);
});
});