Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created October 4, 2017 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simenbrekken/f97e7a7be0b1dc06238c6d3ef0ff64ea to your computer and use it in GitHub Desktop.
Save simenbrekken/f97e7a7be0b1dc06238c6d3ef0ff64ea to your computer and use it in GitHub Desktop.
Signal handling for node Express apps in Docker
const PORT = process.env.PORT || 8081
const HOST = process.env.HOST || 'localhost'
const app = express()
const server = app.listen(PORT, err => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Listening at http://${HOST}:${PORT}`)
})
const handleSignal = (signal, message) => {
process.on(signal, () => {
console.info(message)
server.close(err => {
if (err) {
console.error(err)
process.exit(1)
}
process.exit()
})
})
}
handleSignal('SIGINT', 'Received Ctrl-C, shutting down gracefully...')
handleSignal('SIGTERM', 'Container is stopping, shutting down gracefully...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment