#stubborndevelopers
In this video, we will learn all about streams in Node.js.
Streams are objects that let you read data from a source or write data to a destination in a continuous fashion. In Node.js, there are four types of streams −
Readable − Which is used for a read operation.
Writable − Which is used for a write operation.
Duplex − Which can be used for both reading and write operation.
Transform − A type of duplex stream where the output is computed based on input.
Each type of Stream is an EventEmitter instance and throws several events at a different instance of times. For example, some of the commonly used events are −
data − This event is fired when there is data is available to read.
end − This event is fired when there is no more data to read.
error − This event is fired when there is an error receiving or writing data.
finish − This event is fired when all the data has been flushed to the underlying system
//Require fs module
const fs = require(“fs”);
//Create writable stream
fs.createWriteStream(“data.txt”);
//Create readable stream
fs.createReadStream(“data.txt”);
#node.js