Node.js https获取请求ECONNRESET

T.O*_*ara 4 javascript https asynchronous node.js

我的问题与此问题类似: Node Js:是否有可能从Node JS服务器发出对某些api的1000 http get请求,但它们对节流的解决方案对我不起作用。

我正在拨打Google App Engine上托管的api /网站的电话,以触发任务来更新特定产品页面的内存缓存。updateCache从调用此函数async.eachSeries。代码非常简单:(这是经过清理的版本)

function updateCache(object_name, callback) {
  var options = {
    host          : 'www.example.com',
    path          : '/update-cache/' + object_name,
    method        : 'GET',
    agent         : false,
  };
  var req = https.request(options, function(res) {
    res.on('data', function() {
      if (res.statusCode !== 200) {
        console.log('successful');
      }
    });
    res.on('end', function() {
      console.log('end');
      callback();
    });
  });
  req.on('error', function(e) {
    console.log('failed');
    console.error(e.stack);
    callback();
  });
  req.end();
}
Run Code Online (Sandbox Code Playgroud)

它可以在我的Mac机器上完美运行,但是我需要该脚本才能在使用Windows 7的Windows PC上运行,这就是出现此错误的原因:

events.js:141
  throw er; // Unhandled 'error' event
  ^

Error: read ECONNRESET
  at exports._errnoException (util.js:870:11)
  at TCP.onread (net.js:552:26)
Run Code Online (Sandbox Code Playgroud)

T.O*_*ara 5

我遵循此解决方案,它看起来像这样:

var req = https.request(options, function(res) {
    res.on('data', function() {
        if (res.statusCode !== 200) {
            // HANDLE
        }
    });
    res.on('end', function() {
        if (res.statusCode !== 200 && app.retry_count < 10) {
            app.retry_count += 1;
            retriggerFunction(param, callback);
        } else {
            app.retry_count = 0;
            return callback();
        }
    });
});
req.on('error', function(e) {
    app.retry_count += 1;
    retriggerFunction(param, callback);
});
req.on('timeout', function(e) {
    app.retry_count += 1;
    retriggerFunction(param, callback);
    req.abort();
});
req.on('uncaughtException', function(e) {
    app.retry_count += 1;
    retriggerFunction(param, callback);
    req.abort();
});
Run Code Online (Sandbox Code Playgroud)

retriggerFunction只是我将其包装在其中的函数。它可能不是“正确的”,但目前对我有用。如果有人对此有所改进,请告诉我