3

I have nodejs http server. After sending one request and getting the response back I'm trying to close it using server.close() but it takes a very long time to close (more than 60s). What can be the problem?

Edit

Currently I'm using chromes advanced rest client for testing, sending one request results with the slow closing after getting the response, as far as I know it should not stuck with keep-alive. I'm definitely closing my response with response.end() after response.write().

Here is some code:

handleRequest = function(request, response) {
    var body = '';
    request.on('data', function (data) {
        body += data;
    });
    request.on('end', function() {
        // get some data from db...
        response.setHeader('Content-Type', 'application/json');
        response.write(result);
        response.end();
    });
});

var server = http.createServer(handleRequest);
server.listen(8000);

process.on('SIGINT', function() {
    if (server != undefined) {
        console.log("Closing Server...");
        server.close(function (e) {
            if (e) {
                throw 'Error on closing server';
            } else {
                console.log("Server closed");
            }
        });
    }
});
pizzaisdavid
  • 455
  • 3
  • 13
irenal
  • 135
  • 2
  • 10

2 Answers2

2

please check that you have close your response, after sending your data.

response.end()

If this doesn't work you can post you code sample.

Ravi Sha
  • 21
  • 2
0

If it is the server process that you want to terminate, you should try process.exit('SIGINT'); or process.exit() right after your response.end(). This will end you server and the execution of your script.

Note that all exit signals excepted for SIGKILL and SIGSTOP can be intercepted by the process.

server.close() only stops the server from accepting new connections and closes all connections connected to this server which are not sending a request or waiting for a response.