Search code examples
javascriptnode.jsapachehttprequesthttp-request

Sending request from node.js to apache2.2.21


I am calling function dorequest many times per request to node server. I have problem with request to webpage running on apache2.2.21. Almost of these request are done without any problems, but several request ending with error ECONNRESET and I don't know why. If I use apapche2.4 then everything going well.

var request = require('request');

function dorequest(set, callback){
    request.get(url, function optionalCallback(err, httpResponse, body){
        if (err){
            console.log(url);
            throw err;
        } else {
            //do some stuffs 
        }
    });
}

Solution

  • Probably your apache server simply drops your request because there are too many connections at the same time initiated by dorequest function.

    You can execute those request consequently by calling one in the callback of another by calling the next request in the callback for the previous one, but since there are quite a lot of them and for estetic reasons I would recommend to use async library - it's awesome and really handy when dealing with things like that.

    function dorequest(set, callback){
        request.get(url, function optionalCallback(err, httpResponse, body){
            if (err){
                callback(err);
            } else {
                //do some stuffs 
            }
            callback(err, res);
        });
    }
    
    var maxRequestAtATime = 30;
    
    async.mapLimit(arrayOfOptions, maxRequestAtATime, dorequest, function(err, results){
        // results is now an array of stats for each request
    });
    

    If the options of a request depend on the options of the previous one, you should use async.waterfall.