-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (44 loc) · 1.5 KB
/
Copy pathserver.js
File metadata and controls
49 lines (44 loc) · 1.5 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
import dotenv from 'dotenv';
dotenv.config({ path: './config.env' });
// Unhandled Exception (a bug in the code)
process.on('uncaughtException', (err) => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
import app from './app.js';
import mongoose from 'mongoose';
// Database connection
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 45000,
})
.then(() => {
console.log('DB connection successful');
});
// Listen to the port
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Unhandled Promise Rejection (a promise that is rejected but not caught like mongoDB connection)
process.on('unhandledRejection', (err) => {
console.log('UNHANDLED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
// close the server first to avoid the request and responses that are running in the background to be terminated and after that exit the application by 1 means exit with failure
server.close(() => {
process.exit(1);
});
});
// A signal that sent to the process to tell it to terminate gracefully in production by vercel, heroku, ...etc.
process.on('SIGTERM', () => {
console.log('👋 SIGTERM RECEIVED. Shutting down gracefully');
server.close(() => {
console.log('💥 Process terminated!');
});
});