node.js 可以检测到它何时关闭吗?

aur*_*gar 5 node.js

我想知道是否可以通过单击 Windows 上的“X”来检测 node.js 进程何时关闭。如果是,那么如何。我试过,process.on("exit");但没有奏效。

Dan*_*iel 4

捕捉SIGHUP

/*
 * Run this code and close the Window before the 10 seconds
 */
var x = setTimeout(function() { console.log('hello world') }, 10000);

process.on('SIGHUP', function() {
  console.log('About to exit');
  process.exit();
});
Run Code Online (Sandbox Code Playgroud)