sudarsan2k5 / Backend

It's time for backend πŸ”₯.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Backend

It's time for backend πŸ”₯.

Introduction to Streams

What is Sreams:-

Strams used to process (read and write) data pices by pices(chuks) without completing the whole read or write operation, and therefore without keeping all the data in memory.

Ex:- NETFLIX You Tube

When we read file using Streams we read part of the data, do something with it, then free our memory and repeat this untile the entire file has been processed.

πŸ‘‰ Perfect for handling large volume of data, for example Videos.

πŸ‘‰ More efficient data processing in terms of memory(no need to keep all data in memory) and time (we don't have to wait until all the data is avaliable).

NODE.JS STREAMS FUNDAMENTALS

➑️ In node there are 4 fundamentals tyoes of streams.

  • Readable Streams
  • Writable Sreams
  • Duplex Streams
  • Transform Streams

πŸ”΄ Readable Streams and Writable Sreams are most Important

image

Streams in Practice

  • Q. Now lets say that for some reason in our application, we need to read a large text file from the file and then send to the client.

          SO HOW WOULD WE DO THAT πŸ€”β“.
    

βœ… SOLUTION 1:-

  const fs = require('fs');
  const server = require('http').createServer();
  
  server.on('request', (req, res) => {
      // Solution 1
      fs.readFile('./index.text', (err, data) => {
          if(err) console.log("Error");
          res.end(data);
      });
  });
  
  server.listen('8000', '127.0.0.1', () => {
      console.log("Listing......");
  });

Hello

About

It's time for backend πŸ”₯.


Languages

Language:JavaScript 100.0%