Express.js shutdown hook

前端 未结 4 591
旧巷少年郎
旧巷少年郎 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条回答
  •  -上瘾入骨i
    2021-01-03 19:02

    If you need to access something in the current scope, you can bind() like in this answer: https://stackoverflow.com/a/14032965/1410035 but you might want to bind 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}));
    

提交回复
热议问题