Windows在node.js中等效于process.on('SIGINT')? [英] What is the Windows equivalent of process.on('SIGINT') in node.js?

查看:88
本文介绍了Windows在node.js中等效于process.on('SIGINT')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循

I'm following the guidance here (listening for SIGINT events) to gracefully shutdown my Windows-8-hosted node.js application in response to Ctrl+C or server shutdown.

但是Windows没有SIGINT.我也尝试过process.on('exit'),但似乎起不到任何作用.

But Windows doesn't have SIGINT. I also tried process.on('exit'), but that seems to late to do anything productive.

在Windows上,此代码为我提供:错误:没有这样的模块

On Windows, this code gives me: Error: No such module

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

在Windows上,此代码可以运行,但是执行任何正常操作为时已晚:

On Windows, this code runs, but is too late to do anything graceful:

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

在Windows上是否存在SIGINT个等效事件?

Is there a SIGINT equivalent event on Windows?

推荐答案

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

You have to use the readline module and listen for a SIGINT event:

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();
});

这篇关于Windows在node.js中等效于process.on('SIGINT')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆