2017-03-01 34 views
0

我有一個託管在Azure上的節點js express應用程序(Microsoft服務器)。出於某種原因,我感覺應用程序偶爾重啓一次,我想趕上並記錄此事件。所以我嘗試這個,但它不起作用:無法檢測節點js中的退出事件

process.on('exit', function (code) { 
    var exitMsg = 'Process exited with code ' + code; 

    // log exitMsg synchronously 
}); 

據我所知,在Windows上有一些問題捕捉退出事件。也許你有一些解決方案?謝謝。

回答

0

我想你可能正在尋找'SIGINT'事件。它會在您的應用程序中斷或取消時發生。

process.on('SIGINT',() => { 
    console.log('Process Interrupted, exiting'); 

    // eventually exit 
    process.exit(); // Add code if necessary 
}); 

但值得注意的是,如果您使用IISNode在IIS中託管您的應用程序,則可能無法工作。

+0

是的,所有平臺都支持SIGINT,通常可以用CTRL + C生成(儘管這可能是可配置的)。當終端原始模式啓用時,它不會生成.' – peteb

+0

@ jfriend00剛剛在'cmd.exe'和'powershell'中測試過,驗證它可以在Windows 7的兩種環境下都能正常工作 – peteb

+1

好吧,我猜是因爲[本文] (http://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js/14861513#14861513)。 – jfriend00