Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS针处理与url和statusCode匹配的异步请求数组_Javascript_Node.js_Asynchronous_Bluebird_Needle.js - Fatal编程技术网

Javascript NodeJS针处理与url和statusCode匹配的异步请求数组

Javascript NodeJS针处理与url和statusCode匹配的异步请求数组,javascript,node.js,asynchronous,bluebird,needle.js,Javascript,Node.js,Asynchronous,Bluebird,Needle.js,目标: checkList: [ { name: "Google", url: "https://www.google.com" }, { name: "Microsoft", url: "https://www.microsoft.com" }, { name: "Github", url: "https://www.github.com" } ] 给定一个URL数组,

目标:

checkList: [
    {
      name: "Google",
      url: "https://www.google.com"
    },
    {
      name: "Microsoft",
      url: "https://www.microsoft.com"
    },
    {
      name: "Github",
      url: "https://www.github.com"
    }
  ]
给定一个URL数组,我需要发送一个请求并获取每个URL的状态码。最后,我需要将该状态码与对象数组中的请求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
    }
  ]
我目前正在使用和来处理异步调用以获取响应代码

问题是我找不到一种方法来匹配请求URL和响应。由于每个结果在不同的时间返回(如预期的),针头响应不包括请求URL,所以我不能这样说。。。“答复是200”。我只能得到一个响应代码列表

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

这是我目前使用的基于我在别处找到的演示/代码片段的代码要点

代码也包含在下面的内联中

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

如果这是其他东西的复制品,我道歉。经过大量研究,我没能找到它。如果解决方法是使用针以外的东西,那就很好了。不过,我还是更喜欢蓝鸟。提前谢谢你

我自己解决了这个问题,我希望这个解决方案对其他人有用。下面是使用
.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);
  });

};

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

我自己已经解决了这个问题,我希望这个解决方案对其他人有用。下面是使用
.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);
  });

};

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

为什么不将代码放在问题本身中,而不是一个会过时的外部链接,这样问题对未来的读者就没有用处了?在第20行。。。如果您说
,我无法访问此处的原始请求URL/路径进行匹配
。。。
results[n]
不是
allpath[n]
@JaromandaX的结果吗?我从来没有打算破坏这个要点,但我已经调整了帖子,将其内联。至于你的问题。。。这是个好问题。我假设,由于异步调用可能在给定网络流量的不同时间返回值,因此无法保证结果数组的顺序与原始请求URL的顺序相同。你知道这样的事情是否有保证吗?似乎有一种更合适的方法可以做到这一点。@JacobHeater-没有尝试过你-只是把它放在那里:p@JacobHeater-谢谢你的建议。是的,我也试过了。在异步承诺链中,该库也看不到任何通过请求路径/url的方法。无论我使用什么异步方法,我唯一能访问的数据是needle.get调用的resp,body结果。为什么不把代码放在问题本身,而不是一个会过时的外部链接中,那么这个问题对未来的读者来说是无用的呢?在第20行。。。如果您说
,我无法访问此处的原始请求URL/路径进行匹配
。。。
results[n]
不是
allpath[n]
@JaromandaX的结果吗?我从来没有打算破坏这个要点,但我已经调整了帖子,将其内联。至于你的问题。。。这是个好问题。我假设,由于异步调用可能在给定网络流量的不同时间返回值,因此无法保证结果数组的顺序与原始请求URL的顺序相同。你知道这样的事情是否有保证吗?似乎有一种更合适的方法可以做到这一点。@JacobHeater-没有尝试过你-只是把它放在那里:p@JacobHeater-谢谢你的建议。是的,我也试过了。在异步承诺链中,该库也看不到任何通过请求路径/url的方法。无论我使用什么异步方法,我唯一可以访问的数据是needle.get调用的resp,body结果。