AceDevHub
Intermediate Node.js Interview QuestionsIntermediateConcept

Node.js · Question 49

What are streams in Node.js?

Direct answer

Streams process data in chunks over time instead of loading everything into memory — Node uses them for files, HTTP bodies, compression, and crypto; pipe() connects readable → writable with backpressure handling.

Streams are Node's answer to large or slow I/O — read a 500MB log file or proxy a download without a 500MB Buffer. They align with non-blocking I/O (Q10) and the event-driven model.

Why interviewers ask

  • Memory efficiency — constant memory vs readFile on huge inputs.
  • Latency — start processing first chunk before entire file arrives.
  • Composability — fs.createReadStream → gzip → http response.
  • Built-in usage — req/res are streams; stdout/stdin are streams.
stream-pipe.mjs
import fs from "node:fs";
import { pipeline } from "node:stream/promises";
import { createGzip } from "node:zlib";

// Stream large interview JSON export instead of readFileSync
await pipeline(
  fs.createReadStream("data/interviews/nodejs/nodejs-interview-questions.json"),
  createGzip(),
  fs.createWriteStream("/tmp/interviews.json.gz")
);