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

前端 未结 6 1342
北海茫月
北海茫月 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:36

    Currently there is still no support in node for capturing the windows console control events, so there are no equivalents to the POSIX signals:

    https://github.com/joyent/node/issues/1553

    However the tty module documentation does give an example of a mechanism to capture the key presses in order to initiate a graceful shutdown, but then this does only work for ctrl+c.

    var tty = require('tty');
    
    process.stdin.resume();
    tty.setRawMode(true);
    
    process.stdin.on('keypress', function(char, key) {
      if (key && key.ctrl && key.name == 'c') {
        console.log('graceful exit of process %d', process.pid);
        process.exit();
      }
    });
    

提交回复
热议问题