Express.js关机挂钩

在Express.js中,是否有设置应用程序closures时执行的callback函数?

你可以像这样使用node.js 核心进程的“exit”事件 :

process.on('exit', function() { // Add shutdown logic here. }); 

当然,在退出函数返回后,主事件循环将停止运行,所以你不能在该函数中调度任何定时器或者callback(例如,任何I / O必须是同步的)。

有process.on('exit',callback) :

 process.on('exit', function () { console.log('About to exit.'); }); 

如果您需要访问当前作用域中的某些内容,则可以像下面的回答那样bind() : https : //stackoverflow.com/a/14032965/1410035但是您可能想要绑定this

 function exitHandler(options, err) { if (options.cleanup) console.log('clean'); if (err) console.log(err.stack); if (options.exit) process.exit(); } process.on('exit', exitHandler.bind(null,{cleanup:true}));