Interacting with paused streams
A Node.js stream can be in either flowing or paused mode. In flowing mode, data chunks are read automatically, whereas in paused mode, the stream.read()
method must be called to read the chunks of data.
In this recipe, we will learn how to interact with a readable stream that is in paused mode, which is its default upon creation.
Getting ready
In the learning-streams
directory created in the previous recipe, create the following file:
$ touch paused-stream.js
We're now ready to move on to the recipe.
How to do it…
In this recipe, we'll learn how to interact with a readable stream that is in paused mode:
- First, import the
fs
module inpaused-stream.js
:const fs = require("fs");
- Next, create a readable stream to read the
file.txt
file using thecreateReadStream()
method:const rs = fs.createReadStream("./file.txt");
- Next, we need to register a
readable
event handler on the readable...