Node.JS Read Write Steams and Pipes


ReadStream


const fs = require('fs');

const stream = fs.createReadStream('lorem.txt', 'utf-8');

let data = '';

// stream.on('data', part => {
//   console.log(part.length);
// });

stream.on('data', part => (data += part));
// stream.on('end', () => console.log('End', data.length));
stream.on('end', () => console.log('End', data));
stream.on('error', error => console.log('Error', error.message));



WriteStream


const fs = require('fs');

const stream = fs.createReadStream('lorem.txt', 'utf-8');
const output = fs.createWriteStream('lorem.md');

let data = '';

stream.on('data', part => output.write(part));
stream.on('error', error => console.log('Error', error.message));




Pipe 1


const fs = require('fs');

const input = fs.createReadStream('lorem.txt');
const output = fs.createWriteStream('lorem.md');

input.pipe(output);



Pipe 2


const fs = require('fs');
const zlib = require('zlib');

const input = fs.createReadStream('lorem.txt');
const output = fs.createWriteStream('lorem.md.gz');
const gzip = zlib.createGzip();

input.pipe(gzip).pipe(output);

// class ReadStream {
//   pipe(stream) {
//     this.on('data', part => stream.write(part));
//     return stream;
//   }
// }