Konstantin Vasilev
Konstantin Vasilev

Reputation: 23

Node.js handles 6 http requests at a 2 minutes time

I have a simple Node.js script that starts a HTTP server on port 1337 and handles GET requests. I send 10 GET requests (in less than a second) to localhost:1337/?number=(number from 1 to 10). The script handles the first 6 requests, then takes a 2 minute break, then handles the 4 remaining requests.

I need to handle the HTTP GET requests immediately when they are sent. How would I do that?

Here is my code:

var url = require('url'),
   http = require('http'),
     qs = require('querystring');

var server = http.createServer(function (request, response) {
  if (request.method == 'GET') {
    var url_params = url.parse(request.url, true);
    var number     = url_params.query.number;

    console.log(number);
  }               
});

server.listen(1337);

Upvotes: 2

Views: 280

Answers (2)

VeXii
VeXii

Reputation: 3109

It's most likely because you never close the connection. Try adding a res.end() call.

Upvotes: 4

npclaudiu
npclaudiu

Reputation: 2441

Try to add the following lines at the end of the request handler:

res.writeHead(200);
res.end();

Upvotes: 2

Related Questions