Node JS - Streams
Streams are the continuous flow of data, It is suitable for large size data transmission between client and server.
Unlike Buffer, Streams does not hold the entire data into the memory, instead it divides the data into multiple smaller data sets called chunks.
And transmits the chunk in a continuous order . It reduces the load from the server. There are different types of streams
Read : Used for reading the stream
Write : Used for writing the stream
Duplex : Used to perform both read and write
Transform : Similar to duplex, but in this the stream can be transform or manipulated, if required and and perform either read or write operation
Example:
const fs = require("fs");
const http = require("http");
http
.createServer((req, res) => {
fs.createReadStream(__filename)
.pipe(res)
.on("error", () => console.log("error reported"));
})
.listen(3000, () => console.log("Connected!!"));

0 Comments