I am running a server using Node.js and need to request data from another server that I am running (localhost:3001). I need to make many requests (~200) to the
Sounds like you're just trying to download a bunch of URLs in parallel. This will do that:
var request = require('request');
var async = require('async');
var urls = ['http://microsoft.com', 'http://yahoo.com', 'http://google.com', 'http://amazon.com'];
var loaders = urls.map( function(url) {
return function(callback) {
request(url, callback);
}
});
async.parallel(loaders, function(err, results) {
if (err) throw(err); // ... handle appropriately
// results will be an array of the results, in
// the same order as 'urls', even thought the operation
// was done in parallel
console.log(results.length); // == urls.length
});
or even simpler, using async.map:
var request = require('request');
var async = require('async');
var urls = ['http://microsoft.com', 'http://yahoo.com', 'http://google.com', 'http://amazon.com'];
async.map(urls, request, function(err, results) {
if (err) throw(err); // handle error
console.log(results.length); // == urls.length
});