forked from dtonon/nstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (26 loc) · 831 Bytes
/
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
import polka from 'polka';
import stoppable from 'stoppable';
const PORT = process.env.PORT || 8001;
const app = polka();
const server = stoppable(app.listen(PORT, (err) => {
if (err) throw err;
console.log(`Server is listening on port ${PORT}`);
}));
let isShuttingDown = false;
const shutdown = () => {
if (isShuttingDown) {
console.error('Shutdown already in progress, ignoring additional signal');
return;
}
isShuttingDown = true;
console.log('Received shutdown signal, closing server gracefully...');
server.stop((err) => {
if (err) {
console.error('Error during server shutdown:', err);
}
console.log('Server closed. Exiting process.');
process.exit(0);
});
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);