【问题标题】:Sending request from node.js to apache2.2.21从 node.js 发送请求到 apache 2.2.21
【发布时间】:2016-02-04 07:08:04
【问题描述】:

对于节点服务器的每个请求,我都会多次调用函数 dorequest。 我对在 apache2.2.21 上运行的网页的请求有问题。几乎这些请求都没有任何问题,但有几个请求以错误ECONNRESET 结尾,我不知道为什么。如果我使用 apapche2.4,那么一切顺利。

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

【问题讨论】:

  • 也许您的 apache 服务器只是因为dorequest 发起的连接过多而直接丢弃了您的请求?
  • 我知道应该是有问题。我该如何解决这种行为?我尝试设置了很多 apache 的设置,但没有任何进展。当我向 apache2.4 发出请求时它是稳定的。
  • 可以帮忙做一些延迟请求队列吗?

标签: javascript node.js apache httprequest http-request


【解决方案1】:

可能您的 apache 服务器只是丢弃了您的请求,因为dorequest 函数同时发起的连接太多。

因此,您可以通过调用前一个回调中的下一个请求来调用另一个回调中的一个来执行这些请求,但是由于它们数量很多并且出于审美原因,我建议使用@987654321 @ - 在处理类似的事情时它很棒而且非常方便。

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

如果一个请求的选项依赖于前一个的选项,你应该使用async.waterfall

【讨论】:

  • 这是有道理的。我试试看。谢谢。
【解决方案2】:

我更新了脚本并为此使用了 async.queue 函数,但在 apache 上仍有一些错误。

    function dorequest(set, callback)
{
     console.log('add request');
    q.push({set: set, callback: callback}, function (err) { }); 
}

var q = async.queue(function (task, callback) {

    setTimeout(function () {
        console.log('hello ' + task.set.url, ' lenght: ',q.length());

        if (task.set.method=='get')
        {               
            myrequest.get(task.set.url, function optionalCallback(err, httpResponse, body) 
            {

                if (err)
                {
                    console.log(task.set.url);
                    throw err;
                }
                else
                {
                    //console.log(set.url,body);
                    if (typeof task.callback !='undefined') task.callback(body);
                    callback();
                }
            });
        }
        else
        {       
            if (!task.set.data) task.set.data={};       
            myrequest.post(task.set.url, function optionalCallback(err, httpResponse, body) 
            {

                if (err)
                {
                    console.log(task.set.url);
                    throw err;
                }
                else
                {
                    //console.log(set.url,body);
                    if (typeof task.callback !='undefined') task.callback(body);
                    callback();
                }
            }).form(task.set.data); 
        }       

    },500); 
},1);

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2014-02-14
    • 2012-03-27
    • 2020-10-30
    相关资源
    最近更新 更多