/
server.js
44 lines (33 loc) Β· 1.22 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
import { connect } from 'mongoose';
import dotenv from 'dotenv';
import app from './app.mjs';
dotenv.config({ path: './config.env' }); // connect environment variable config.env file
process.on('uncaughtException', (error) => {
console.log('UNCAUGHT EXCEPTION! π₯ Shutting down...');
console.log(error);
process.exit(1);
});
const databaseConnectionString = process.env.DATABASE_CONNECTION_STRING.replace(
'<password>',
process.env.DATABASE_PASSWORD,
).replace('<dbname>', process.env.DATABASE_NAME);
connect(databaseConnectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('DB connection succesful!'))
.catch((error) => console.log(error));
const port = process.env.PORT || 6969;
const server = app.listen(port, () => {
console.log(`The server is running in port ${port} !`);
});
// Unhandled promise rejections
process.on('unhandledRejection', (error) => {
console.log(error);
console.log('Unhandled promise rejections!!! Shutting down...');
// shutdown gracefully with first close the server, and then shutdown the application
// server.close give server time to finish all the requests before shutdown the app
server.close(() => {
process.exit(1);
});
});