Creating streams in Node.js
The Node.js stream API is provided by the Node.js stream
core module. This recipe will provide an introduction to 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.
The code samples for this chapter are available in the Packt GitHub repository (https://github.com/PacktPublishing/Node.js-14-Cookbook) in the Chapter03
directory.
Getting ready
- 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 move on to the recipe.
How to do it…
In this recipe, we'll learn how to create both a readable stream and a writeable stream. We'll first create a writable stream to write a large file. We'll then read that large file using a readable stream:
- Start by importing...