Created
June 25, 2018 02:59
-
-
Save evaporei/92777f54aa73b501216863d5e1fe8123 to your computer and use it in GitHub Desktop.
graceful-shutdown/express-with-graceful.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
app.get('/wait', (req, res) => { | |
console.log('Started wait!') | |
setTimeout(() => { | |
res.send('Done!') | |
console.log('Done!') | |
}, 10000) | |
}) | |
const setupGracefulShutdown = (httpServer) => { | |
process.on('SIGHUP', () => { | |
console.log('SIGHUP happened!') | |
httpServer.close(() => { | |
console.log('server closed!') | |
process.exit(128 + 1) | |
}) | |
}) | |
process.on('SIGINT', () => { | |
console.log('SIGINT happened!') | |
httpServer.close(() => { | |
console.log('server closed!') | |
process.exit(128 + 2) | |
}) | |
}) | |
process.on('SIGTERM', () => { | |
console.log('SIGTERM happened!') | |
httpServer.close(() => { | |
console.log('server closed!') | |
process.exit(128 + 15) | |
}) | |
}) | |
} | |
const server = app.listen(8000) | |
setupGracefulShutdown(server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment