I'm learning nodeJS and I was looking at code regarding streaming data from text. More specifically how to write to streams and piping to a writable stream (if I'm saying that correct). These are the two pieces of code are as follows:
var fs = require('fs');
var readable = fs.createReadStream(__dirname + '/greet.txt', { encoding: 'utf8', highWaterMark: 16 * 1024 });
var writable = fs.createWriteStream(__dirname + '/greetcopy.txt');
readable.on('data', function(chunk) {
console.log(chunk);
writable.write(chunk);
});
var fs = require('fs');
var zlib = require('zlib');
var readable = fs.createReadStream(__dirname + '/greet.txt');
var writable = fs.createWriteStream(__dirname + '/greetcopy.txt');
var compressed = fs.createWriteStream(__dirname + '/greet.txt.gz');
var gzip = zlib.createGzip();
readable.pipe(writable);
readable.pipe(gzip).pipe(compressed);
This is a noob question but are the .on(), .write() and .pipe() methods built into Nodejs itself (so it applies to all variables) or is it somehow added on when the variables writable/readable are assigned the functions in the fs module? I'm guessing this applies to other methods I will come across while learning.
via DanT29
No comments:
Post a Comment