Graceful Shutdown - SIGTERM
suggest changeserver.close() stops new connections and runs its callback after existing ones finish. process.exit() then ends the process. A terminal Ctrl+C sends SIGINT, not SIGTERM, so listen for both signals when you want the same shutdown from the keyboard.
var http = require('http');
var server = http.createServer(function (req, res) {
setTimeout(function () { //simulate a long request
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}, 4000);
}).listen(9090, function () {
console.log('listening http://localhost:9090/');
console.log('pid is ' + process.pid);
});
function shutdown() {
server.close(function () {
process.exit(0);
});
}
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents