This page looks best with JavaScript enabled

write and read streams everything you need to know?

 ·  ☕ 3 min read  ·  👽 john hashim

node js Streams are collections of data — just like arrays or strings. The difference is that streams might not be available all at once, and they don’t have to fit in memory. This makes streams really powerful when working with large amounts of data, or data that’s coming from an external source one chunk at a time.

1
2
3
4
5
6
7
8
const fs = require('fs');
const file = fs.createWriteStream('./file.txt');
//1e6 = 1000000
for(let i=0; i<= 1e6; i++) {
  file.write('We can Change\n');
}

file.end();

fs.createReadStream() allows you to open up a readable stream in a very simple manner. All you have to do is pass the path of the file to start streaming in. It turns out that the response (as well as the request) objects are streams.

Share on

john hashim
WRITTEN BY
john hashim
Web Developer