Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js http server takes long time to close

Tags:

node.js

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");
            }
        });
    }
});
like image 377
irenal Avatar asked Aug 14 '26 19:08

irenal


1 Answers

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.

like image 123
Ravi Sha Avatar answered Aug 22 '26 01:08

Ravi Sha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!