Stop node.js program from command line

后端 未结 19 2286
无人及你
无人及你 2020-11-29 14:51

I have a simple TCP server that listens on a port.

var net = require(\"net\");

var server = net.createServer(function(socket) {
    socket.end(\"Hello!\\n\"         


        
19条回答
  •  一向
    一向 (楼主)
    2020-11-29 15:01

    Though this is a late answer, I found this from NodeJS docs:

    The 'exit' event is emitted when the REPL is exited either by receiving the .exit command as input, the user pressing -C twice to signal SIGINT, or by pressing -D to signal 'end' on the input stream. The listener callback is invoked without any arguments.

    So to summarize you can exit by:

    1. Typing .exit in nodejs REPL.
    2. Pressing -C twice.
    3. pressing -D.
    4. process.exit(0) meaning a natural exit from REPL. If you want to return any other status you can return a non zero number.
    5. process.kill(process.pid) is the way to kill using nodejs api from within your code or from REPL.

提交回复
热议问题