Quitting node.js gracefully

后端 未结 5 629
天涯浪人
天涯浪人 2020-11-30 19:27

I\'m reading through the excellent online book http://nodebeginner.org/ and trying out the simple code

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

function onRequest(req         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 20:08

    I currently use Node's event system to respond to signals. Here's how I use the Ctrl-C (SIGINT) signal in a program:

    process.on( 'SIGINT', function() {
      console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
      // some other closing procedures go here
      process.exit( );
    })
    

    You were getting the 'Address in Use' error because Ctrl-Z doesn't kill the program; it just suspends the process on a unix-like operating system and the node program you placed in the background was still bound to that port.

    On Unix-like systems, [Control+Z] is the most common default keyboard mapping for the key sequence that suspends a process (SIGTSTP).[3] When entered by a user at their computer terminal, the currently running foreground process is sent a SIGTSTP signal, which generally causes the process to suspend its execution. The user can later continue the process execution by typing the command 'fg' (short for foreground) or by typing 'bg' (short for background) and furthermore typing the command 'disown' to separate the background process from the terminal.1

    You would need to kill your processes by doing a kill or 'killall -9 node' or the like.

提交回复
热议问题