Interview Q&A / Node.js

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How do you send a large file to the browser without loading it all in memory?

SHORT ANSWER

Create a read stream of the file and pipe it into the HTTP response. Set the Content-Type and, if known, Content-Length headers first, and handle stream errors before any bytes are sent.

Explain it simply

Using readFile and res.send for a 500 MB video forces the server to hold the whole file in RAM for every viewer. With a stream the server handles it like a water pipe: a little at a time goes from disk to the network. Ten viewers cost ten small buffers, not ten copies of the file.

Example

js
const express = require("express");
const fs = require("fs");
const path = require("path");
const { pipeline } = require("stream");

const app = express();

app.get("/lectures/:name", (req, res, next) => {
  const file = path.join(__dirname, "videos", path.basename(req.params.name));

  fs.stat(file, (err, stats) => {
    if (err) return res.status(404).send("Not found");

    res.setHeader("Content-Type", "video/mp4");
    res.setHeader("Content-Length", stats.size);

    pipeline(fs.createReadStream(file), res, (error) => {
      if (error) console.error("stream failed", error.message);
    });
  });
});

app.listen(3000);

Details:

  • path.basename stops a request like ../../secret from escaping the folder.
  • pipeline destroys the file stream if the user closes the tab, so the descriptor is freed.
  • For video seeking, support the Range header; res.sendFile does this for you.

What to say in the interview

"I pipe fs.createReadStream into res with pipeline, set content headers up front, and sanitise the filename. That keeps memory flat and cleans up if the client disconnects. res.sendFile is the simpler option for ordinary files."

Common mistakes

  • Using readFile for big downloads.
  • Joining user input into the path without sanitising it.

#nodejs#streams#express

Related questions

Practise more Node.js questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Node.js quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.