简体   繁体   中英

node.js http server takes long time to close

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");
            }
        });
    }
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM