The pipe function in Node and http.get

I learn node in workshops from nodeschool . learnyounode workshop name, question number 8. HTTP COLLECT .

Question: Write a program that executes an HTTP GET request to the URL provided to you as the first command line argument. Collect all the data from the server (not just the first “data" event), and then write two lines to the console (Standard output). The first line you write should be an integer representing the number of characters received from the server. The second line should contain the full line of characters sent by the server.

The answer I presented was as follows.

var http = require('http');
var url = process.argv[2];
http.get(url,function(res){
    var body = '';
    res.on('error',function(err){
        console.error(err);
    })
    res.on('data',function(chunk){
        body+=chunk.toString();
    });
    res.on('end',function(){
        console.log(body.length);
        console.log(body);
    });
});

while the answer they provided was

var http = require('http')
var bl = require('bl')
http.get(process.argv[2], function (response) {
  response.pipe(bl(function (err, data) {
    if (err)
      return console.error(err)
    data = data.toString()
    console.log(data.length)
    console.log(data)
  }))
})


. , http.get() pipe...

+4
1

, . , body. pipe response bl ( ), , , . "" , bl .

pipe - , , .

: . , ...

+3

All Articles