What are streams in Node.js
· Category: Node.js
Short answer
Streams are objects that let you read data from a source or write data to a destination continuously, processing chunks incrementally instead of loading entire files into memory.
How it works
Node.js provides four types of streams: Readable, Writable, Duplex, and Transform. Data flows through a stream in chunks (Buffers). You can listen for data events on readable streams and write chunks to writable streams. Piping connects a readable stream directly to a writable stream.
Example
const fs = require('fs');
const readable = fs.createReadStream('large.txt');
const writable = fs.createWriteStream('copy.txt');
readable.pipe(writable);
Why it matters
Streams are essential for performance when handling large files, network requests, or real-time data. They keep memory usage low and allow processing to begin before all data has arrived.