How to Stream Large Files in Node.js
How to process massive video or data files without crashing your server memory.
Identify the Memory Trap
Using standard fs.readFile loads the entire file into the server's RAM. If multiple users download a 2GB file simultaneously, the server will crash with an Out of Memory exception.
Create a Readable Stream
Instead of reading the whole file, use fs.createReadStream('file.mp4'). This opens a conduit to the file, reading it in small 64KB chunks.
Pipe to the HTTP Response
The Express 'res' object is actually a Writable Stream. Call readStream.pipe(res). This immediately sends each 64KB chunk directly to the client as soon as it's read from the disk.
Manage Backpressure Automatically
By using the .pipe() method, Node.js automatically manages backpressure. If the user's internet connection is slow, Node will pause reading from the disk until the network buffer clears.
Handle Stream Errors
Attach an error event listener: readStream.on('error', (err) => {...}). If the file doesn't exist or permissions fail, you must catch the error and send a 500 response, otherwise the server might crash.
Ready to master this completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

