在Node.js中的单个HTTP请求中调用多个HTTP请求 [英] Calling multiple HTTP requests in a single HTTP request in Node.js

查看:184
本文介绍了在Node.js中的单个HTTP请求中调用多个HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在单个URL调用中调用多个URL,并将其推送到数组中,并发送该数组以响应最终用户。

I am trying to call multiple URL in a single URL call and push it's json response in an array and send that array in response to the end user.

我的代码如下所示:

var express = require('express');

var main_router = express.Router();

var http = require('http');

urls = [
"http://localhost:3010/alm/build_tool",
"http://localhost:3010/alm/development_tool",
"http://localhost:3010/alm/project_architecture"];

var responses = [];

main_router.route('/')

.get(function (req, res) {

var completed_requests = 0;

for (url in urls) {

  http.get(url, function(res) {

    responses.push(res.body);

    completed_request++;

    if (completed_request == urls.length) {

        // All download done, process responses array
    }
  });
}
res.send(responses);
});

我也尝试过使用npm请求模块。
当我运行这个代码时,它只返回NULL或一些只有头文件的随机输出。

I have also tried this using npm request module. When i run this code it only return NULL or some random output that have only headers.

我的目标是在单个节点获取请求中调用多个URL并将其JSON输出附加到数组上并发送给最终用户。

My aim is to call multiple URL's in a single node get request and append it's JSON output on a array and send to the end user.

谢谢

推荐答案

这里,尝试这个代码,

const async = require('async');
const request = require('request');

function httpGet(url, callback) {
  const options = {
    url :  url,
    json : true
  };
  request(options,
    function(err, res, body) {
      callback(err, body);
    }
  );
}

const urls= [
  "http://localhost:3010/alm/build_tool",
  "http://localhost:3010/alm/development_tool",
  "http://localhost:3010/alm/project_architecture"
];

async.map(urls, httpGet, function (err, res){
  if (err) return console.log(err);
  console.log(res);
});

说明:
此代码使用同步请求节点包。根据定义,async.map 需要3个参数,第一个是数组,第二个是要使用该数组的每个元素调用的迭代器函数,以及回调函数当async.map已经完成处理。

Explanation : This code uses async and request node packages. async.map by definition takes 3 params, first one being an array, second being the iterator function you want to call with each element of that array, and the callback function, called when async.map has finished processing.


map(arr,iterator,[callback])

通过将arr中的每个值映射到
的迭代器函数,生成一个新的数组。使用arr
中的一个项目调用迭代器,并且在完成处理后调用一个回调函数。这些
回调中的每一个都有两个参数:一个错误,并且转换的项目从
arr。如果迭代器将错误传递给其回调,则主回调
(对于地图函数)将立即被调用并出现错误。

Produces a new array of values by mapping each value in arr through the iterator function. The iterator is called with an item from arr and a callback for when it has finished processing. Each of these callback takes 2 arguments: an error, and the transformed item from arr. If iterator passes an error to its callback, the main callback (for the map function) is immediately called with the error.

注意:对iterator函数的所有调用都是并行的。

Note: All calls to iterator function are parallel.

在你的httpGet函数里面,你调用请求函数传递url,并明确地将响应格式告诉 json 请求,完成处理后,使用三个参数调用回调函数,err - 如果有的话,res-server响应,身体响应体。
如果没有 err 请求 async.map 将这些回调的结果作为数组收集,并将该数组结束传递给第三个回调函数。否则,如果(err)为true,则 async.map 函数停止执行,并使用 err 调用其回调。

Inside your httpGet function, you are calling request function with passed url, and explicitly telling the response format to be json. request, when finished processing, calls the callback function with three params, err - if any, res - server response, body - response body. In case there is no err from request, async.map collects the results from these callbacks as an array, and passes that array at the end to its third, callback function. Otherwise,if (err) is true, the async.map function stops the execution and calls its callback with an err.

这篇关于在Node.js中的单个HTTP请求中调用多个HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆