Node.js Course
Node.js
/
Intermediate

Pipe Method

Definition

A method available on all Readable streams that automatically funnels its data into a Writable stream, automatically managing data flow and backpressure.

Explain Like I'm New

Piping is literally plumbing. If you have a hose shooting water (Readable Stream) and an empty pool (Writable Stream), the `.pipe()` method connects them. It automatically makes sure the hose doesn't shoot water faster than the pool can drain. If the pool gets full, `.pipe()` automatically pauses the hose.

Real World Example

A user asks to download a massive video from your server. Instead of loading the video into RAM, you do: `fs.createReadStream('video.mp4').pipe(res)`. The file streams directly from the hard drive straight out to the user's internet connection flawlessly.

Common Use Cases

  • Streaming files to HTTP responses
  • Connecting multiple Transform streams (like reading -> zipping -> encrypting -> writing)

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What does `.pipe()` do?

intermediate

  • How does `.pipe()` handle backpressure?

Flash Cards

Question

What does it do?

Click to reveal answer
Answer

It attaches a Readable stream to a Writable stream, automatically passing data from one to the other.

Question

How does it handle backpressure?

Click to reveal answer
Answer

If the Writable stream is too slow, its internal buffer fills up and it returns `false`. The `.pipe()` mechanism detects this and automatically calls `.pause()` on the Readable stream. Once the Writable stream drains, it emits a `'drain'` event, and `.pipe()` automatically calls `.resume()` on the Readable stream.