A stream in Node.js is a conceptual framework for handling continuous data flow. That means you can consume data as it is loaded or produced, chunk by chunk (or piece by piece), instead of getting it all into memory to start consuming it. Streams can be readable, writable, or both.

Contents

  1. Types of streams
  2. The pipe method

Types of streams

  • Readable stream, which data can be read.
  • Writable stream, which data can be written.
  • Duplex stream, which is both Readable and Writable.
  • Transform stream, which is a Duplex stream that can modify or transform the data as it is written and read.

Streams are present in many Node.js modules, for example http.request(), zlib.createGzip(), fs.createReadStream(), process.stdout. All of these return streams.

The pipe method

The pipe method allows you to connect the output of the readable stream as the input of the writable stream:

const fs = require('fs');
const readableStream = fs.createReadStream('input.txt');
const writableStream = fs.createWriteStream('output.txt');
readableStream.pipe(writableStream);

In this example, we create stream objects for reading and writing files, and then pass data from the read stream to the write stream using the pipe() method. As data comes into the read stream, it’s automatically passed to the write stream without having to explicitly handle each piece of data.

This method redirects, or as they often say, “pipes” data from a readable stream to a writable stream, ensuring that its internal buffer does not overflow. Thus, pipe simplifies the process of transferring data between stream objects and allows you to work with large amounts of data without loading everything into your computer’s memory.

If you pipe into a duplex stream you can chain to other stream:

readable.pipe(duplex).pipe(writable);