Express.js shutdown hook

前端 未结 4 589
旧巷少年郎
旧巷少年郎 2021-01-03 18:37

In Express.js, is there someway of setting a callback function to be executed when the application shuts down?

4条回答
  •  借酒劲吻你
    2021-01-03 18:56

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

提交回复
热议问题