Node.js 多个http get请求节点.js
这就是我的代码对于单个http get请求的外观。我要做的是调用另外两个http get请求并存储它们的结果,以便能够适当地进行渲染。有人能建议我如何实现这一目标吗?谢谢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',
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
}