在Express.js中,是否有一种设置回调函数在应用关闭时执行的方法?
You could use the node.js core process 'exit' event like so:
process.on('exit', function() {
// Add shutdown logic here.
});
当然,在 exit 函数返回后,主事件循环将停止运行,因此您无法从该函数内部安排任何计时器或回调(例如,任何 I/O 必须是同步的)。
async function()
吗? - Hendy Irawanasync
。 - emc在 Node.js 中有 process.on('exit', callback) 事件:
process.on('exit', function () {
console.log('About to exit.');
});
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}));
const exitHandler = (options, err) => {...}
- Eric Haynes