1. Concept:

2. Basic Example: Reading a File Using a Stream


Real-World Use Case:

Imagine you're building a file-upload API that processes multi-GB files on the fly without exhausting RAM. You’d pipe the incoming data stream to a processing stream and write it to disk/database.


Interview Questions:


Gotcha Question:

What happens here?

const fs = require('fs');
const stream = fs.createReadStream('hugeFile.txt');
stream.on('data', (chunk) => {
  console.log('Chunk:', chunk.length);
});
stream.on('end', () => {
  console.log('File read complete');
});

Follow-up: Now imagine console.log is slow (e.g. logging to remote server). What problem might occur?

Answer: Memory can build up — backpressure isn’t handled; console.log is the bottleneck.


Code Challenge: Custom Transform Stream

Write a custom transform stream that converts all input to uppercase.