Streams in Node.js

  • Post category:Node
  • Reading time:1 mins read

Use of streams:

It will fix the problem of backpressure that means it will automatically handle the speed of data coming in and the data going out.

Streams in Node

const fs = require("fs");
const server = require("http").createServer();

server.on("request", (req, res) => {
    const readableSource = fs.createReadStream("myFile.txt");
    readableSource.pipe(res);
});

server.listen(8000, "127.0.0.1", () => {
    console.log("Listen...");
});

 

Share this