Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:39:01+00:00 2026-06-18T02:39:01+00:00

I have a Node.js application that contains an http(s) server. In a specific case,

  • 0

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

In a specific case, I need to shut down 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 shuts down the server, but only after a minimum wait time of 120 seconds. But I want the server to shut down 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:39:02+00:00Added an answer on June 18, 2026 at 2:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a node application that is not a web application - it completes
I have a node.js (v0.6.12) application that starts by evaluating a Javascript file, startup.js.
I use node.js and socket.io. I have an application that runs on the IP
Currently i have a node.js and socket.io application in development on my local machine
I have an application that makes use of a canvas that contains several WPF
I have a nodejs application that I run like this, over SSH: $ tmux
I have a little Node.js application that I'd like to hit a remote API
I have a node application I am trying to host on Azure, that create
I have an application that I am currently writing that works by iterating through
I have an XML file within a Silverlight application that contains application settings and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.