使用 nodeJS 进行异步 http 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17106622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:46:55  来源:igfitidea点击:

Asynchronous http calls with nodeJS

node.jshttpnode-async

提问by Ludo

I would like to launch asynchronous http calls on my server node, i saw the asyncnode module and i guess the async.parallelenables us to do that.

我想在我的服务器节点上启动异步 http 调用,我看到了async节点模块,我想这async.parallel使我们能够做到这一点。

The documented example is pretty clear, but i don't know how i could manage multiple http calls.

记录的示例非常清楚,但我不知道如何管理多个 http 调用。

I tried the example bellow but it doesn't even launch the http calls:

我尝试了下面的示例,但它甚至没有启动 http 调用:

var http = require('http');

var Calls = [];
Calls.push(function(callback) {
    // First call
    http.get('http://127.0.0.1:3002/first' callback);
});

Calls.push(function(callback) {
    // Second call
     http.get('http://127.0.0.1:3002/second' callback);
});

var async = require('async');
async.parallel(Calls, function(err, results) {
    console.log('async callback: '+JSON.stringify(results));
    res.render('view', results);
});

If i launch the http requests separately, i do have a result, but but calling the async callback i get async callback: [null,null]

如果我单独启动 http 请求,我确实有结果,但是调用异步回调我得到 async callback: [null,null]

回答by freakish

Have a look at the documentation:

看看文档

With http.request() one must always call req.end() to signify that you're done with the request - even if there is no data being written to the request body.

使用 http.request() 必须始终调用 req.end() 以表示您已完成请求 - 即使没有数据写入请求正文。

You are creating a request, but you are not finalizing it. In your calls you should do:

您正在创建一个请求,但尚未完成它。在您的通话中,您应该执行以下操作:

var req = http.request(options, function(page) {
    // some code
});
req.end();

This is assuming you are doing a normal GET request without body.

这是假设您正在执行没有正文的正常 GET 请求。

You should also consider using http.getwhich is a nice shortcut:

您还应该考虑使用http.get,这是一个不错的快捷方式:

http.get("http://127.0.0.1:3002/first", function(res) {
    // do something with result
});

UpdateThe other thing is that callbacks in async have to be of the form

更新另一件事是异步中的回调必须采用以下形式

function(err, res) { ... }

The way you are doing it now won't work, because callback to http.get accepts only one argument res. What you need to do is the following:

您现在的做法行不通,因为对 http.get 的回调只接受一个参数res。您需要做的是:

http.get('http://127.0.0.1:3002/second', function(res) {
    callback(null, res);
});

回答by Jacek Pietal

dont use capital names for other purpouses than types/classes

不要将大写名称用于类型/类以外的其他目的

below is your code with corrected obvious mistakes

下面是你的代码,更正了明显的错误

var http = require('http');

var calls = [];
calls.push(function(callback) {
    // First call
    http.get('http://127.0.0.1:3002/first', function (resource) {
         resource.setEncoding('utf8');
         resource.on('data', function (data) {
             console.log('first received', data);
             callback();
         });
    });
});

calls.push(function(callback) {
    // Second call
    http.get('http://127.0.0.1:3002/second', function (resource) {
         resource.setEncoding('utf8');
         resource.on('data', function (data) {
             console.log('second received', data);
             callback();
         });
    });
});

var async = require('async');
async.parallel(calls, function(err, results) {
    console.log('async callback ', results);
    res.render('view', results);
});

回答by Ludo

Ok the thing is to call the callback this way callback(null, res);instead callback(res);, i think the first parameter is interpreted as an error and the second one is the real result.

好东西是调用回调函数这种方式callback(null, res);来代替callback(res);,我想到的第一个参数被解释为错误,第二个才是真正的结果。