Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I shutdown a Node.js http(s) server immediately?

I have a Node.js application that contains an http(s) server.

In a specific case, I need to shutdown this server programmatically. What I am currently doing is calling its close() function, but this does not help, as it waits for any kept alive connections to finish first.

So, basically, this shutdowns the server, but only after a minimum wait time of 120 seconds. But I want the server to shutdown immediately - even if this means breaking up with currently handled requests.

What I can not do is a simple

process.exit(); 

as the server is only part of the application, and the rest of the application should remain running. What I am looking for is conceptually something such as server.destroy(); or something like that.

How could I achieve this?

PS: The keep-alive timeout for connections is usually required, hence it is not a viable option to decrease this time.

like image 902
Golo Roden Avatar asked Jan 31 '13 13:01

Golo Roden


People also ask

How do I stop HTTP server in NodeJS?

close() : server. stop() stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event.

How do you stop a NodeJS process?

Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.

How do I close a Web server?

To stop the server, I just press Ctrl+C.

How do I close a NodeJS browser?

Use a websocket connection for the NodeJS server to instruct the HTML page on the client to execute window. close() Have the HTML page periodically check (perhaps with a GET request) the server to see if the page is ready to execute window. close()


1 Answers

The trick is that you need to subscribe to the server's connection event which gives you the socket of the new connection. You need to remember this socket and later on, directly after having called server.close(), destroy that socket using socket.destroy().

Additionally, you need to listen to the socket's close event to remove it from the array if it leaves naturally because its keep-alive timeout does run out.

I have written a small sample application you can use to demonstrate this behavior:

// Create a new server on port 4000 var http = require('http'); var server = http.createServer(function (req, res) {   res.end('Hello world!'); }).listen(4000);  // Maintain a hash of all connected sockets var sockets = {}, nextSocketId = 0; server.on('connection', function (socket) {   // Add a newly connected socket   var socketId = nextSocketId++;   sockets[socketId] = socket;   console.log('socket', socketId, 'opened');    // Remove the socket when it closes   socket.on('close', function () {     console.log('socket', socketId, 'closed');     delete sockets[socketId];   });    // Extend socket lifetime for demo purposes   socket.setTimeout(4000); });  // Count down from 10 seconds (function countDown (counter) {   console.log(counter);   if (counter > 0)     return setTimeout(countDown, 1000, counter - 1);    // Close the server   server.close(function () { console.log('Server closed!'); });   // Destroy all open sockets   for (var socketId in sockets) {     console.log('socket', socketId, 'destroyed');     sockets[socketId].destroy();   } })(10); 

Basically, what it does is to start a new HTTP server, count from 10 to 0, and close the server after 10 seconds. If no connection has been established, the server shuts down immediately.

If a connection has been established and it is still open, it is destroyed. If it had already died naturally, only a message is printed out at that point in time.

like image 122
Golo Roden Avatar answered Oct 22 '22 20:10

Golo Roden