小编典典

Windows在node.js中等效于process.on('SIGINT')?

node.js

我正在遵循此处的指导(侦听SIGINT事件)以响应Ctrl+C或服务器关闭来正常关闭Windows-8托管的node.js应用程序。

但是Windows没有SIGINT。我也尝试过process.on('exit'),但这似乎迟迟没有任何成效。

在Windows上,此代码为我提供: 错误:无此类模块

process.on( 'SIGINT', function() {
  console.log( "\ngracefully shutting down from  SIGINT (Crtl-C)" )
  // wish this worked on Windows
  process.exit( )
})

在Windows上,此代码可以运行,但现在 做任何优雅的动作为时已晚

process.on( 'exit', function() {
  console.log( "never see this log message" )
})

SIGINTWindows上是否有等效事件?


阅读 512

收藏
2020-07-07

共1个答案

小编典典

您必须使用readline模块并监听SIGINT事件:

http://nodejs.org/api/readline.html#readline_event_sigint

if (process.platform === "win32") {
  var rl = require("readline").createInterface({
    input: process.stdin,
    output: process.stdout
  });

  rl.on("SIGINT", function () {
    process.emit("SIGINT");
  });
}

process.on("SIGINT", function () {
  //graceful shutdown
  process.exit();
});
2020-07-07