-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (50 loc) · 1.59 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
49
50
51
52
53
54
55
56
57
58
/* IMPORT MODULE //////////////////// */
import './app/utils/config.js';
import http from 'http';
/* IMPORT UNCAUGHT EXCEPTION //////////////////// */
import './app/errors/uncaughtException.js';
/* IMPORT APP //////////////////// */
import app from './app/app.js';
// flag to check if the shutdown process has started
let isShuttingDown = false;
/* CREATE SERVER //////////////////// */
const server = http.createServer(app);
const { SITE_PROTO, SITE_HOST, SITE_PORT } = process.env;
/* UNHANDLED REJECTION //////////////////// */
process.on('unhandledRejection', (reason, _) => { // removed param 'promise'
console.error('UNHANDLED REJECTION! 💥 Shutting down...');
console.error(reason);
isShuttingDown = true;
// close server connection
server.close(() => {
process.exit(1);
});
});
/* START SERVER //////////////////// */
try {
server.listen(SITE_PORT, () => {
const date = new Date().toLocaleString('fr-FR', { timeZone: 'Europe/Paris' });
console.log(`${date} - Application running at ${SITE_PROTO}://${SITE_HOST}:${SITE_PORT}`);
});
} catch (err) {
console.error('Error starting server:', err);
process.exit(1);
}
/* SIGTERM //////////////////// */
process.on('SIGTERM', () => {
if (isShuttingDown) return;
console.log("Received 'SIGTERM' signal, shutting down...");
isShuttingDown = true;
server.close(() => {
process.exit(0);
});
});
/* SIGINT //////////////////// */
process.on('SIGINT', () => {
if (isShuttingDown) return;
console.log("Received 'SIGINT' signal, shutting down...");
isShuttingDown = true;
server.close(() => {
process.exit(0);
});
});