Node.js 无法检测节点js中的退出事件

Node.js 无法检测节点js中的退出事件,node.js,azure,Node.js,Azure,我在Azure(Microsoft服务器)上托管了一个节点js express应用程序。出于某种原因,我感觉应用程序偶尔会重新启动一次,我想捕捉并记录这个事件。所以我尝试了这个,但不起作用: process.on('exit', function (code) { var exitMsg = 'Process exited with code ' + code; // log exitMsg synchronously }); 我知道在Windows上捕获退出事件时出现一些问

我在Azure(Microsoft服务器)上托管了一个节点js express应用程序。出于某种原因,我感觉应用程序偶尔会重新启动一次,我想捕捉并记录这个事件。所以我尝试了这个,但不起作用:

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

    // log exitMsg synchronously
});

我知道在Windows上捕获退出事件时出现一些问题。也许你有办法?谢谢。

我想您可能正在寻找
'SIGINT'
活动。当你的应用程序被中断或取消时,就会发生这种情况

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

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

不过值得注意的是,如果您使用IISNode在IIS中托管应用程序,这可能无法正常工作。

在azure/win上,这可能会有所帮助:

if (process.platform === "win32") {
  require("readline")
    .createInterface({
      input: process.stdin,
      output: process.stdout
    })
    .on("SIGINT", function () {
      process.emit("SIGINT");
    });
}

是的,在所有平台上都支持来自终端的
SIGINT,并且通常可以使用CTRL+C生成(尽管这可能是可配置的)。启用terminal raw模式时不会生成它。
@jfriend00刚刚在
cmd.exe
powershell
中进行了测试,验证了它在Windows 7OK上的两种环境下都能正常工作,我想它是从那时起添加的。