spinners
spinners

Reputation: 2679

Is it possible to pipe to console.log?

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');
  });
});

Upvotes: 37

Views: 53123

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32127

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);

Upvotes: 75

Related Questions