streams is process(read and write) data piece(chunks) without completing the
whole read or write operation, and therefore without keeping all the data in
memory.
There are four types of streams => readable streams, writable streams, duplex
and transform streams.
const { Readable } = require("stream")
const readable = Readable.from(["input string"])
readable.on("data", (chunk) => {
console.log(chunk) // will be called once with `"input string"`
})
const fs = require('fs');
const r = fs.createReadStream('file.txt');
const z = zlib.createGzip();
const w = fs.createWriteStream('file.txt.gz');
r.pipe(z).pipe(w);
const stream = require('stream')
const fs = require('fs')
const html = "<h1> Hello Wordl </h1>
// read file from string and write file
const stream = new Stream()
stream.pipe = function (dest) {
dest.write(html)
return dest
}
stream.pipe(createWriteStream(resolve(process.cwd(), 'index.html'), 'utf8'))
// read file from directory and write file
fs.createReadStream('testing.html')
.pipe(createWriteStream(resolve(process.cwd(), 'index.html'), 'utf8'))