Thank you for taking the time to read my question!
I'm new to Node.js and I'm trying to figure out how to output the elements of an array to the console when a server response has come to an end.
This is the code:
var http = require('http');
http.get(process.argv[2], function (response) {
response = response.setEncoding('utf8');
var data = [];
response.on('data', function (collect) {
data.push(collect);
});
response.on('error', function(err) {
console.log(err);
});
});
Where and how do I use response.end event to print out all of the elements of the data array using console.log() method?
Thank you very much for your help!
response.end(function() { for (var i = 0; i < data.length; i++) { console.log(data[i]); } });but it does not work as response.end is not a function.response.on('end',, also since the chunks (yours collect) is string it would make sence to dodata += collectinstead ofpushthis assumes you define data as stringvar data = '';