Asynchronous http calls with nodeJS

2019-02-14 14:27发布

问题:

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

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

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

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]

回答1:

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.

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.

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

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

Update The 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('http://127.0.0.1:3002/second', function(res) {
    callback(null, res);
});


回答2:

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


回答3:

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.