Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created June 25, 2018 02:59
Show Gist options
  • Save evaporei/92777f54aa73b501216863d5e1fe8123 to your computer and use it in GitHub Desktop.
Save evaporei/92777f54aa73b501216863d5e1fe8123 to your computer and use it in GitHub Desktop.
graceful-shutdown/express-with-graceful.js
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