Asynchronous HTTP calls with nodeJS

I would like to run asynchronous HTTP calls on my node server, I saw the async node module, and I think async.parallel allows us to do this.

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

I tried the example below, but it doesn't even launch 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 run the HTTP requests individually, I have the result, but, but by calling the async callback: [null,null] , I get the async callback: [null,null]

+4
source share
3 answers

See the documentation :

With http.request (), you always need to call req.end () to indicate that you are finished with the request - even if there is no data that is written to the request authority.

You create a request, but you do not complete it. In your calls you must:

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

This assumes that you are making a normal GET request without a body.

You should also consider http.get , which is a good shortcut:

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

Update Another thing is that callbacks in async should look like

 function(err, res) { ... } 

The way you do this will no longer work, because the http.get callback accepts only one res argument. What you need to do is the following:

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

dont use capital names for purpouses other than types / classes

below is your code with obvious bug fixes

 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); }); 
+1
source

It's good that you need to call a callback this way callback(null, res); instead of callback(res); , I think that the first parameter is interpreted as an error, and the second is the real result.

0
source

Source: https://habr.com/ru/post/1486213/


All Articles