What is the Windows equivalent of process.on('SIGINT') in node.js?

前端 未结 6 1331
北海茫月
北海茫月 2020-11-28 02:47

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

6条回答
  •  爱一瞬间的悲伤
    2020-11-28 03:48

    Unless you need the "readline" import for other tasks, I would suggest importing "readline" once the program has verified that it's running on Windows. Additionally, for those who might be unaware - this works on both Windows 32-bit and Windows 64-bit systems (which will return the keyword "win32"). Thanks for this solution Gabriel.

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

提交回复
热议问题