Streams
Streams let you process data piece by piece instead of loading it all into memory at once. This is essential for handling large files or network data efficiently.
Node.js has four stream types: Readable, Writable, Duplex, and Transform.
Readable & Writable streams
A Readable stream emits chunks of data as they become available; a Writable stream accepts chunks and writes them out (like to a file or HTTP response).
Piping streams
The `.pipe()` method connects a readable stream directly to a writable one, handling backpressure automatically.
const fs = require('fs');
const readStream = fs.createReadStream('big.txt', 'utf8');
readStream.on('data', chunk => console.log('Chunk:', chunk.length));Chunk: 65536
Chunk: 65536
...The file is read in chunks instead of all at once.
const fs = require('fs');
fs.createReadStream('input.txt').pipe(fs.createWriteStream('output.txt'));pipe() streams data directly from the source file to the destination file.
Key points
- Streams process data in chunks, saving memory.
- Readable streams emit data; writable streams consume it.
- pipe() connects streams and manages backpressure.
- Streams are ideal for large files and network I/O.
