I need to do some useful things when my Express.js service is stopped by SIGINT
. Using Express.js version 3.0.6, I understand that this should work:
var express = require('express');
var app = express();
var server = app.listen(3000);
process.on('SIGINT', function() {
console.log('Do something useful here.');
server.close();
});
But the process doesn't give me back the Bash prompt unless I issue SIGINT
(Control-C) twice:
$ node problem.js
^CDo something useful here.
^CDo something useful here.
net.js:1046
throw new Error('Not running');
^
Error: Not running
at Server.close (net.js:1046:11)
at process.<anonymous> (/path/to/problem.js:8:10)
at process.EventEmitter.emit (events.js:93:17)
at SignalWatcher.startup.processSignalHandlers.process.on.process.addListener.w.callback (node.js:486:45)
$
One more caveat. If I start this Express.js service and don't send any requests then SIGINT
terminates properly.
Clearly there is something fundamental that I'm missing here?
process.exit()
is like sending a ^C after you did one thing and should not be considered gracefully (imho). I think a more complete answer is below (although a little late). – Subway