As we saw in the DOM, streams give us the ability to control the flow of data and be able to process data in a way that creates a nonblocking system. We can see this by creating a simple stream. Let's go ahead and utilize one of the built-in streams that comes with Node.js, readFileStream. Let's write the following script:
import fs from 'fs';import { PassThrough } from 'stream'const str = fs.createReadStream('./example.txt');const pt = new PassThrough();str.pipe(pt);pt.on('data', (chunk) => { console.log(chunk);});
Here, we have imported the fs library and the PassThrough stream from the stream library. Then, we created a read stream for the example.txt file, as well as a PassThrough stream.
A PassThrough stream allows ...