Problem. This is a problem like in the learnyounode JugglingAsync module from node.js. "This problem is the same as the previous problem (HTTP COLLECT) in that you need to use http.get (). However, this time you will be provided with three URLs as the first three command line arguments.
You must collect the full content provided to you by each URL and print it to the console (stdout). You do not need to print the length, just the data as a string; one line per URL. The trick is that you must print them in the same order as the URLs provided to you as the line arguments command. "
I tried using the node.js stream.readable class to respond from the first URL to the second and the response from the third. I expected this to work synchronously, that is, when the first request is completed, the second request will be transmitted through the channels. I use the bl package ( https://www.npmjs.org/package/bl ) to collect all the response data for a request for receipt. Code snippet below:
var https = require('http');
var bl = require('bl');
var finalString = '';
https.get( process.argv[2], function(response)
{
response.setEncoding('utf8');
response.pipe(bl(function (err, data)
{
console.log("First request called");
if (err) return console.error(err);
console.log(data.toString());
})).pipe(bl(function(err, data)
{
console.log("Second Request called");
https.get (process.argv[3], function( response)
{
response.setEncoding('utf8');
response.pipe(bl( function (err, data)
{
if (err) return console.error(err);
console.log(data.toString());
}))
}).on('error', function(err)
{
console.log(err);
})
})).pipe( bl(function(err,data)
{
console.log("Third request called");
https.get (process.argv[4], function( response)
{
response.setEncoding('utf8');
response.pipe(bl( function (err, data)
{
if (err) return console.error(err);
console.log(data.toString());
}))
}).on('error', function(err)
{
console.log(err);
})
})
)
}).on('error', function (err)
{
console.log(err);
}
);
The output does not match the sequence of requests. What am I doing wrong?
source
share