I am trying to learn node.js.
I am trying to understand streams and piping.
Is it possible to pipe the response of http request to console.log?
I know how to do this by binding a handler to the data event but I am more interested in streaming it to the console.
http.get(url, function(response) { response.pipe(console.log); response.on('end', function() { console.log('finished'); }); });
Of course, console. log() will reduce your program's performance since it takes computational time.
To create a multi line strings when printing to the JavaScript console, you need to add the new line character represented by the \n symbol. Alternatively, you can also add a new line break using Enter when you use the template literals syntax.
Yes, structured logging is a best practice.
console.log
is just a function that pipes the process stream to an output.
Note that the following is example code
console.log = function(d) { process.stdout.write(d + '\n'); };
Piping to process.stdout does exactly the same thing.
http.get(url, function(response) { response.pipe(process.stdout); response.on('end', function() { console.log('finished'); }); });
Note you can also do
process.stdout.write(response);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With