The script that you have written ends on the last argument passed in on the command line.
Using node 4.2.4 in debug I ran:
node debug test.js google.com facebook.com yahoo.com
Use c to continue.
repl
to access the context while debugging.
s to step into a function.
And the loop ended with "yahoo.com" each time.
For loops are asynchronous using node, and as suggested in the duplication link you would need to bind
or create a closure on the i
counter. However, with a change in how the loop is being iterated:
var bl = require('bl');
var http = require('http');
var urls = [process.argv[2], process.argv[3], process.argv[4]]
var dataArray = [];
var count = 0;
urls.forEach(function(url, i){
console.log(url)
var options = {
hostname: url,
port: 80
}
http.get(options, function(response){
console.log(options, response.statusCode)
response.setEncoding('utf8').pipe(bl(function (err, data) {
dataArray[i] = data.toString();
count++;
console.log(options, data)
}));
})
.on('error', function(e) {
console.log("Got error: " + e.message);
});
})
The result:
< google.com
< facebook.com
< yahoo.com
< { hostname: 'google.com', port: 80 } 301
< { hostname: 'google.com', port: 80 } <Buffer 3c 48 54 4d 4c 3e 3c 48 45 41 44 3e 3c 6d 65 74 61 20 68 74 74 70 2d 65 71 75 69 76 3d 22 63 6f 6e 74 65 6e 74 2d 74 79 70 65 22 20 63 6f 6e 74 65 6e ... >
< { hostname: 'yahoo.com', port: 80 } 301
< { hostname: 'yahoo.com', port: 80 } <Buffer 3c 48 54 4d 4c 3e 0a 3c 48 45 41 44 3e 0a 3c 54 49 54 4c 45 3e 45 72 72 6f 72 3c 2f 54 49 54 4c 45 3e 0a 3c 2f 48 45 41 44 3e 0a 0a 3c 42 4f 44 59 20 ... >
< { hostname: 'facebook.com', port: 80 } 302
< { hostname: 'facebook.com', port: 80 } <Buffer >
Note I added options
since without including "http://" as part of the url I got:
< google.com
< facebook.com
< yahoo.com
< Got error: connect ECONNREFUSED 127.0.0.1:80
< Got error: connect ECONNREFUSED 127.0.0.1:80
< Got error: connect ECONNREFUSED 127.0.0.1:80
Please note:
Or using promises:
var bl = require('bl');
var http = require('http');
var urls = [process.argv[2], process.argv[3], process.argv[4]]
var dataArray = [];
var count = 0;
for (var i = 0; i <= urls.length-1; i++) {
console.log(i)
// Or you can use a promise which will create a closure with
// the callback passed in
// uncomment this promise to see the difference
// Promise.resolve(i).then(function(i){
console.log(i, urls)
http.get({hostname: urls[i], port:80}, function(response){
response.setEncoding('utf8').pipe(bl(function (err, data) {
dataArray[i] = data.toString();
count++;
console.log(urls[i], i, data)
}));
}).on('error', function(e){
console.log(e.message)
});
// });
};
without promise:
< 0
< 0 [ 'http://google.com',
< 'http://facebook.com',
< 'http://yahoo.com' ]
< 1
< 1 [ 'http://google.com',
< 'http://facebook.com',
< 'http://yahoo.com' ]
< 2
< 2 [ 'http://google.com',
< 'http://facebook.com',
< 'http://yahoo.com' ]
< undefined 3 <Buffer 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 ... >
< undefined 3 <Buffer 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 ... >
< undefined 3 <Buffer 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 ... >
with promise:
< 0
< 1
< 2
< 0 [ 'http://google.com',
< 'http://facebook.com',
< 'http://yahoo.com' ]
< 1 [ 'http://google.com',
< 'http://facebook.com',
< 'http://yahoo.com' ]
< 2 [ 'http://google.com',
< 'http://facebook.com',
< 'http://yahoo.com' ]
< http://google.com 0 <Buffer 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 ... >
< http://facebook.com 1 <Buffer 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 ... >
< http://yahoo.com 2 <Buffer 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 ... >