I see this method in use in many cases but I cannot understand what it does.
An example:
var net = require('net');
var server = net.createServer(function(connection) {
console.log('client connected');
connection.on('end', function() {
console.log('client disconnected');
});
connection.write('Hello World!\r\n');
connection.pipe(connection);
});
server.listen(8080, function() {
console.log('server is listening');
});
When is the connection.pipe(connection);
do?
What I commented this line out, the behavior sees the same.
Another example:
var fs = require("fs");
var zlib = require('zlib');
// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('input.txt.gz'));
console.log("File Compressed.");
Can anybody explain what does the pipe method do?
The documentation is not clear. thanks in advance.