Creating readable and writable streams
The Node.js stream
core module provides the Node.js stream API. This recipe will introduce using streams in Node.js. It will cover how to create both a readable stream and a writable stream to interact with files using the Node.js core fs
module.
Getting ready
Before diving into this recipe, we must set up our workspace by creating a directory and files:
- First, let’s create a directory to work in:
$ mkdir learning-streams $ cd learning-streams
- Create the following two files:
$ touch write-stream.js $ touch read-stream.js
Now, we’re ready to start this recipe.
How to do it…
In this recipe, we’ll learn how to create both a readable stream and a writeable stream. First, we’ll create a writable stream so that we can write a large file. After, we’ll read that large file using a readable stream:
- Start by importing the Node.js core File system module into
write-stream.js
:const...