Streaming
Readable Stream
var fs = require("fs");
var { Readable } = require("stream");
var data = '';
// Create a readable stream to read the file
var readerStream = fs.createReadStream('file.txt');
readerStream.setEncoding('UTF8'); // Set the encoding to be utf8.
// Create a readable stream from string
var readerStream = Readable.from("");
// push the chunks into stream
for(let i = 0 ; i < 1 ; i++) {
stream.push("123")
}
console.log("stream end");
// end the stream
stream.push(null);
// listen the data from the readable stream
readerStream.on('data', function(chunk) {
data += chunk;
});
// listen the stream is finished
readerStream.on('end',function() {
console.log(data);
});
// listen to the error
readerStream.on('error', function(err) {
console.log(err.stack);
});
// consume the content of chunk from readable stream
for await(const chunk of readerStream){
console.log(chunk);
}
console.log("Program Ended");
Write Stream
Pipe
Duplex Stream

Reference
Last updated