Piping streams
A pipe is a form of one-way redirection. In our terminal (DOS or Unix-like), we often utilize the pipe operator (|
) to pipe the output of one program as the input to another program. For example, we can enter $ ls | head -3
to pipe the output of the ls
command to the head -3
command, resulting in the first three files in our directory being returned.
Like how we can use the pipe operator in our shells to pipe output between programs, we can use the Node.js pipe()
method to pipe data between streams.
In this recipe, we’ll learn how to use the pipe()
method.
Getting ready
Follow these steps:
- Create a directory to work in:
$ mkdir piping-streams $ cd piping-streams
- Start by creating a file named
file.txt
:$ touch file.txt
- Add some dummy data to
file.txt
, such as the following:Node.js is a JavaScript runtime built on Google Chrome's V8 JavaScript engine. Node.js is a JavaScript runtime built on Google Chrome's V8 JavaScript engine...