javascript - Moz API 调用超时

标签 javascript node.js api express

我正在使用 Node 来发出 Moz API 请求。这是我的代码:

function MozCall(callback) {
  var mozCall = '';

  // Set your expires times for several minutes into the future.
  // An expires time excessively far in the future will not be honored by the Mozscape API.
  // Divide the result of Date.now() by 1000 to make sure your result is in seconds.
  var expires = Math.floor(Date.now() / 1000) + 300;
  var accessId = 'key';
  var secretKey = 'key';

  // `bitFlagExampleValues` is a list of bitFlag values as strings that we'll
  // loop over and sum together using helper function: `sumColumnValues`
  var bitFlagExampleValues = [
    '144115188075855872',
    '68719476736',
    '34359738368',
  ];
  var sumColumnValues = function(bitFlagValues) {
    return bitFlagValues.reduce(function(accu, bitFlag) {
      var accuValBig = new bigJs(accu);
      var bitFlagBig = new bigJs(bitFlag);
      var bigSum = accuValBig.plus(bitFlagBig);

      return bigSum.toString();
    }, 0);
  };

  // 'cols' is the sum of the bit flags representing each field you want returned.
  // Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
  // returns "144115291155070976"
  var cols = sumColumnValues(bitFlagExampleValues);

  // Put each parameter on a new line.
  var stringToSign = accessId + '\n' + expires;

  //create the hmac hash and Base64-encode it.
  var signature = crypto
    .createHmac('sha1', secretKey)
    .update(stringToSign)
    .digest('base64');
  //URL-encode the result of the above.
  signature = encodeURIComponent(signature);

  var postData = JSON.stringify([
    website,
    'www.apple.com',
    'www.pizza.com',
  ]);

  var options = {
    hostname: 'lsapi.seomoz.com',
    path:
      '/linkscape/url-metrics/?Cols=' +
      cols +
      '&AccessID=' +
      accessId +
      '&Expires=' +
      expires +
      '&Signature=' +
      signature,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': postData.length,
    },
  };

  var responseData = '';

  var req = http.request(options, function(response) {
    response.setEncoding('utf8');
    response.on('data', function(chunk) {
      responseData += chunk;
    });
    response.on('end', function() {
      console.log(responseData);
    });
  });
  req.end()
}
// if (req.user.isPremium == false) {
let freeReportCalls = [MozCall];
// Free user - Single report
// let website = req.body.website0;
async.parallel(freeReportCalls, function(err, results) {
  if (err) {
    console.log(err);
  } else {
    console.log(results);
    res.render('reports/report', {
      title: 'Report',
      // bw: JSON.parse(results[0]),
      // ps: JSON.parse(results[0]),
      // bw: results[1],
      // al: results[2],
      // moz: results[2],
      user: req.user,
    });
  }
});

我从他们的 github 示例中获取了这段代码,可以在这里找到:https://github.com/seomoz/SEOmozAPISamples/blob/master/javascript/node/batching-urls-sample.js

但是每次我调用电话时都会收到此错误。

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

Error: socket hang up
    at createHangUpError (_http_client.js:331:15)
    at Socket.socketOnEnd (_http_client.js:423:23)
    at emitNone (events.js:111:20)
    at Socket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

由于 event.js 是包的一部分,我非常确定我做错了什么,但我不知道是什么。我尝试破解 API key ,但看起来它只是不想连接到 Moz 服务器。知道这里出了什么问题吗?

编辑:我找到了这个讨论,但我不确定它是否与问题相关https://github.com/Automattic/knox/issues/116

Edit2:我添加了一个 req.end() 来解决超时问题。现在我收到 503,服务暂时不可用。知道他们在谈论哪种服务吗?

最佳答案

好的,在最后添加 req.end() 解决了超时问题。我将针对我的新问题另开一篇文章。

关于javascript - Moz API 调用超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541108/

相关文章:

javascript - 全屏损坏 - 库存工具

javascript - 如何解析json对象来更新HANA表?

python - 使用 ElementTree 从 XML 中提取数据时出现问题

json - 如何获得当前的万事达卡汇率(买入/卖出货币)?

javascript - 如何使 MarkerClusters 使用 Pane

使用 val().match() 方法的 Javascript 正则表达式

javascript - D3退出选错数据

c++ - node.js 内部 : How can I find out where `process.binding(' eval')` gets defined?

javascript - 根据用户时区返回本地货币代码

javascript - Node.js 未定义的属性