Node.js Streams Explained
Ad
What are Streams?
Streams process data piece by piece instead of loading it all into memory. This lets Node handle gigabyte files or live data with tiny memory usage.
The Problem Streams Solve
// ❌ Loads the ENTIRE file into RAM — crashes on huge files
const data = fs.readFileSync("10gb.log");
// ✅ Streams it in small chunks — constant low memory
fs.createReadStream("10gb.log")
.on("data", chunk => process(chunk))
.on("end", () => console.log("done"));
Four Types of Streams
- Readable — source you read from (file read, HTTP request).
- Writable — destination you write to (file write, HTTP response).
- Duplex — both (TCP socket).
- Transform — modifies data as it passes (gzip, encryption).
Piping Streams Together
const fs = require("fs");
const zlib = require("zlib");
// Read → compress → write, all streamed
fs.createReadStream("input.txt")
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream("input.txt.gz"));
Real Use Case: File Download Server
app.get("/download", (req, res) => {
fs.createReadStream("big-video.mp4").pipe(res);
// Streams the video without buffering it in memory
});
FAQs
When should I use streams?
Any time data is large or arrives over time — file processing, uploads/downloads, video, CSV parsing.
What does backpressure mean?
When the writable side is slower than the readable side. pipe() handles backpressure automatically. More in our Node.js section.
