5

I'm actually facing a problem with my javascript code executed with node.js i need to send http requests in a loop to a distant server (i set www.google.ca in the code). Here is my code :

var http = require('http');

var options = {
    hostname: 'www.google.ca',
    port: 80,
    path: '/',
    method: 'GET'
};

function sendRequest(options){
    console.log('hello');
    var start = new Date();
    var req = http.request(options,function(res) {
        console.log('Request took:', new Date() - start, 'ms');
    });
    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });
    req.end();
};

for(var i=0;i<10;i++){
    sendRequest(options);
}

The problem I have is that, no matter how many times i go through my loop, i get a response for only the 5 first of them. For the rest of the requests, the function sendRequest() is called but I don't get any responses, neither error message. And then the program terminates. However it works fine when I set localhost as a host. Is anyone would have a solution to this problem ? Thanks in advance !

robav2309
  • 55
  • 1
  • 5
  • possible duplicate of [node.js http.get hangs after 5 requests to remote site](http://stackoverflow.com/questions/16965582/node-js-http-get-hangs-after-5-requests-to-remote-site) – loganfsmyth Jan 24 '14 at 01:33
  • Remote server is restricting number of parallel requests. Try sending these requests in series. – umair Jan 24 '14 at 07:04

1 Answers1

4

perhaps either your machine or the remote machine is getting overwhelmed by the 10 simultaneous requests you make. try sending them one at a time, you will have to wait until the first request completes before continuing. one easy way to do so is with async.timesSeries

var http = require('http');
var async = require('async');

var options = {
  hostname: 'www.google.ca',
  port: 80,
  path: '/',
  method: 'GET'
};

function sendRequestWrapper(n, done){
  console.log('Calling sendRequest', n);
  sendRequest(options, function(err){
    done(err);
  });
};

function sendRequest(options, callback){
  //console.log('hello');
  var start = new Date();
  var req = http.request(options,function(res) {
    // I don't know if this callback is called for error responses
    // I have only used the `request` library which slightly simplifies this
    // Under some circumstances you can accidentally cause problems by calling
    // your callback function more than once (e.g. both here and on('error')

    console.log('Request took:', new Date() - start, 'ms');
    callback(null);
  });
  req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
    callback(err);
  });
  req.end();
};

async.timesSeries(10, sendRequestWrapper);
bookcasey
  • 39,223
  • 13
  • 77
  • 94
Plato
  • 10,812
  • 2
  • 41
  • 61