Express.js shutdown hook
There is process.on('exit', callback):
process.on('exit', function () {
console.log('About to exit.');
});
You could use the node.js core process 'exit' event like so:
process.on('exit', function() {
// Add shutdown logic here.
});
Of course, the main event loop will stop running after the exit function returns so you can't schedule any timers or callbacks from within that function (e.g. any I/O must be synchronous).