Building stream pipelines
The Node.js core stream
module provides a pipeline()
method. Similar to how we can use the Node.js core stream pipe()
method to pipe one stream to another, we can also use the pipeline()
method to chain multiple streams together.
Unlike the pipe()
method, the pipeline()
method also forwards errors, making it easier to handle errors in the stream’s flow.
This recipe builds upon many of the stream concepts that were covered in the other recipes in this chapter. Here, we’ll create a stream pipeline using the pipeline()
method.
Getting ready
Before diving into this recipe, let’s set up our workspace by creating a directory and files:
- First, create a directory to work in named
stream-pipelines
:$ mkdir stream-pipelines $ cd stream-pipelines
- Create a file named
pipeline.js
:$ touch pipeline.js
- We’ll also need some sample data to transform. Create a file named
file.txt
:$ touch file.txt
- Add some dummy text data...