Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pipe to console.log?

Tags:

stream

node.js

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');   }); }); 
like image 397
spinners Avatar asked Jun 26 '14 09:06

spinners


People also ask

Does console log reduce performance?

Of course, console. log() will reduce your program's performance since it takes computational time.

How do you add a line in console log?

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.

Is console log a good practice?

Yes, structured logging is a best practice.


1 Answers

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); 
like image 121
Ben Fortune Avatar answered Oct 12 '22 00:10

Ben Fortune