Use Streams in Node.js | Create Readable & Writable Streams in Node JS

Опубликовано: 11 Октябрь 2024
на канале: Stubborn Developers
594
25

#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");

*********** Handle Events in Node.js ***********
   • Events Module in Node.js | Handle Eve...  

*********** Use fs module synchronously in Node.js ***********
   • File System Module fs in Node.js | No...  

*********** Use fs module Asynchronously in Node.js ***********
   • Node.js Asynchronous File System Modu...  

*********** Node.JS Tutorial in English 2021 Playlist ***********
   • how to install node js  

*********** React.JS Tutorial in Hindi 2021 Playlist ***********
   • react js introduction in hindi