javascript - NodeJS Needle 处理与 url 和 statusCode 匹配的异步请求数组

标签 javascript node.js asynchronous bluebird needle.js

目标:

给定一组 URL,我需要发送一个请求并获取每个 URL 的 statusCode。最后,我需要将该 statusCode 与对象数组中的请求 URL 进行匹配,然后将其返回到页面。

像这样:

checkList: [
    {
      name: "Google",
      url: "https://www.google.com"
    },
    {
      name: "Microsoft",
      url: "https://www.microsoft.com"
    },
    {
      name: "Github",
      url: "https://www.github.com"
    }
  ]

对于列表中的每个 URL,我只需发出一个 http 请求并返回一个状态代码(例如:200)并将其与请求路径进行匹配。所以最终的输出应该是这样的......

results: [
    {
      name: "Google",
      url: "https://www.google.com",
      status: 200
    },
    {
      name: "Microsoft",
      url: "https://www.microsoft.com",
      status: 200
    },
    {
      name: "Github",
      url: "https://www.github.com",
      status: 200
    }
  ]

我目前正在使用BluebirdJSNeedle处理异步调用以获取响应代码。

问题是我找不到将请求 URL 与响应相匹配的方法。由于每个结果在不同时间返回(按预期),并且 Needle 响应不包含请求 URL,因此我不能说类似...“https://google.com 响应是 200”。我只能获取响应代码列表。

我对高级 JS 比较陌生,所以也许有一种方法可以覆盖回调/ promise ,让我能够在整个过程中传递请求 URL,但我不知道如何实现。

这是我当前使用的代码要点,基于我在其他地方找到的演示/片段... https://gist.github.com/sgelliott/13840b6f2f9d2ab481fcded6dc4a9188

代码还包含在下面的内联...

var needle = require('needle');
var Promise = require("bluebird");
Promise.promisifyAll(needle);

var allPaths = ["https://google.com", "https://microsoft.com", "https://github.com"];
var current = Promise.resolve();

Promise.map(allPaths, function(path) {
    current = current.then(function() {
        console.log("path: " + path); // path shows up here correctly - it's available
        return needle.getAsync(path, options);
    });
    return current;
}).map(function(resp, body) {
    return {
        path: "path goes here",
        status: resp.statusCode
    };
}).then(function(results) {
    console.log("results: " + JSON.stringify(results)); // this outputs 'resulst' : [{"path":"path goes here","status":200},{"path":"path goes here","status":302},{"path":"path goes here","status":200}]
    return renderResults(results); //this simply sets 'statusResults' to 'results' for the next res.send to use - will refine once I solve the path matching issue
}).then(function() {
    console.log('All Needle requests are processed!');
    res.send(statusResults); //
}).catch(function(e) {
    console.log(e);
});

如果这是与其他内容重复的内容,我深表歉意。经过大量研究后我无法找到它。如果解决方案是使用 Needle 以外的其他东西,那完全没问题。不过我还是更愿意选择 Bluebird 。预先感谢您!

最佳答案

我自己解决了这个问题,希望该解决方案对其他人有用。这是使用 .bind

运行的更新代码
module.exports.gethealth = function (req, res) {
  var statusResults = [];
  var resultCount = 0;
  var allPaths = [];
  for (var item in config.checkList) {
    allPaths[item] = config.checkList[item].url;
  }

  var options = {connection: 'keep-alive'};

  Promise.map(allPaths, function (path) {
    console.log("path: " + path);

    return needle.getAsync(path, options)
      .bind(this, path)
      .then(function (resp, body) {
        statusResults[resultCount] = {path:path, status:resp.statusCode};
        resultCount++;
      });
  }).then(function () {
    res.send(statusResults);
  });

};

如果您有任何后续问题,请告诉我。

关于javascript - NodeJS Needle 处理与 url 和 statusCode 匹配的异步请求数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42570266/

相关文章:

javascript - Visual Studio 2012 : compile more TypeScript files to one JavaScript file

javascript - 获取被拖动的 SVG 元素的 id

node.js - 如何同步 Node 中的两个异步调用?

python - 有人可以像我五岁一样向我解释 python-twisted 吗?

javascript - Node.js 和 postgres 听

rest - 何时使用 JMS,何时使用 REST?

javascript - 我们可以将 .filter() 函数与搜索属性的条件规则一起使用吗?

javascript - AngularJS,指令

javascript - 将 jsPDF 与 Electron 结合使用

node.js - Node js Mongoose 使用 mongoose-encryption 加密数据