node.js - Cant make async work with http in nodejs -
i have come across weird problem using async , http in nodejs. here code :
http = require('http'); bl = require('bl'); async = require('async'); var tasks = []; var dataitems = []; var callhttp = function(index, callback) { http.get(process.argv[index], function(response) { response.pipe(bl(function(err, data) { if (err) return console.log(err); dataitems[index] = data.tostring(); })); response.on('end', callback); }) .on('error', console.error); } var indices = [2, 3, 4, 5]; indices.foreach(function(item) { tasks.push(function(callback) { callhttp(item, callback); }); }); async.parallel(tasks, function(err) { console.log(dataitems); });
running above code using
node async-http.js 'http://www.google.com/' 'http://academics.vit.ac.in/' 'http://www.example.com'
gives me following error:
{ [error: connect econnrefused] code: 'econnrefused', errno: 'econnrefused', syscall: 'connect' }
however, running same code in different form (by manually defining array 'tasks') seems work fine ... :
http = require('http'); bl = require('bl'); async = require('async'); var tasks = []; var dataitems = []; var callhttp = function(index, callback) { http.get(process.argv[index], function(response) { response.pipe(bl(function(err, data) { if (err) return console.log(err); dataitems[index] = data.tostring(); })); response.on('end', callback); }) .on('error', console.error); } async.parallel([ function(callback) { callhttp(2, callback); }, function(callback) { callhttp(3, callback); }, function(callback) { callhttp(4, callback); }], function(err) { console.log(dataitems); });
any idea doing wrong? have searched everywhere , found no answers yet
process.argv[5]
not exists.
as process.argv[5]
supposed fourth argument. , have provided 3 process arguments. , making http
request undefined
url. , connection refused.
Comments
Post a Comment