It's time for backend π₯.
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).
β‘οΈ 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
-
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......");
});