Express.js shutdown hook

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).

Leave a Comment