Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Node.js 多个http get请求节点.js_Node.js_Asynchronous - Fatal编程技术网

Node.js 多个http get请求节点.js

Node.js 多个http get请求节点.js,node.js,asynchronous,Node.js,Asynchronous,这就是我的代码对于单个http get请求的外观。我要做的是调用另外两个http get请求并存储它们的结果,以便能够适当地进行渲染。有人能建议我如何实现这一目标吗?谢谢 router.get('/get_all_posts', function(req, res){ var body =''; var options = { host: 'localhost', path: '/some_endpoint',

这就是我的代码对于单个http get请求的外观。我要做的是调用另外两个http get请求并存储它们的结果,以便能够适当地进行渲染。有人能建议我如何实现这一目标吗?谢谢

router.get('/get_all_posts', function(req, res){
        var body ='';

        var options = {
            host: 'localhost',
            path: '/some_endpoint',
            method: 'GET',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        };


        var request = http.request(options, function(response) {
            response.setEncoding('utf8');

            response.on('data', function (data_chunk) {
                body += data_chunk;
            });

            response.on('end', function () {

                if(response) {
                    res.render("something");
                }
                else
                {
                    res.render('error', {error: {status:400,stack:body}});
                }

            });
        });
        request.end();
    });

您不应该发出http请求。对于
/some\u endpoint
的任何代码都将其转换为函数,然后在路由器内部为
/some\u endpoint
/get\u all\u posts
调用该函数

希望能有所帮助。

这对我很有用

async.map([options1, options2, options3], reqCall, function(err, results){
            if ( err){
                // do something
            } else {
                   results[0]//first call
                   results[1]//second call
                   results[2] //third call
           }