Asynchronous http calls with nodeJS

Ludo picture Ludo · Jun 14, 2013 · Viewed 28.8k times · Source

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]

Answer

freakish picture freakish · Jun 14, 2013

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