Tutorial

Streaming File Uploads in Node

Stream multipart uploads straight to storage without buffering in memory, enforce size limits mid-stream, and abort on limit breach.

On this page

  1. Introduction
  2. Use a streaming parser
  3. Pipe to storage
  4. Abort on limit
  5. Backpressure
  6. Validate the type
  7. Test both sides of the limit
  8. Sources

Introduction

This guide covers streaming file uploads in Node without buffering, a recurring concern in the file-upload scenario. Buffering an upload in memory caps your file size at available RAM and lets a hostile client exhaust the heap. Streaming the multipart parts straight to storage removes the ceiling, and a size check that fires mid-stream aborts an oversized upload before it lands.

Use a streaming parser

Reach for busboy over multer when sizes are unbounded, because it emits each file as a stream you pipe onward instead of buffering the whole part.

The trade-off worth naming is that reach for busboy over multer when sizes are unbounded, because it emits each file as a stream you pipe onward instead of buffering the whole part adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import busboy from 'busboy';
export function handle(req, res) {
  const bb = busboy({ headers: req.headers, limits: { fileSize: 10*1024*1024 } });
  bb.on('file', (name, stream, info) => {
    stream.on('limit', () => { res.status(413).end('too big'); stream.destroy(); });
    stream.pipe(storage.createWriteStream(info.filename));
  });
  req.pipe(bb);
}

Pipe to storage

Pipe the part stream straight to a storage write stream so the bytes flow through your process without accumulating, which keeps memory flat regardless of file size.

The trade-off worth naming is that pipe the part stream straight to a storage write stream so the bytes flow through your process without accumulating, which keeps memory flat regardless of file size adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Abort on limit

Listen for the limit event, destroy the stream, and respond 413, so an oversized upload is rejected the moment it crosses the line, not after it lands.

The trade-off worth naming is that listen for the limit event, destroy the stream, and respond 413, so an oversized upload is rejected the moment it crosses the line, not after it lands adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Backpressure

Let Node backpressure pause the client when storage is slow, instead of buffering in your process, which is the other half of staying flat in memory.

The trade-off worth naming is that let node backpressure pause the client when storage is slow, instead of buffering in your process, which is the other half of staying flat in memory adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Validate the type

Sniff the first bytes of the stream for the real type rather than trusting the Content-Type header, which a client can set to anything, and reject early on mismatch.

The trade-off worth naming is that sniff the first bytes of the stream for the real type rather than trusting the content-type header, which a client can set to anything, and reject early on mismatch adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { fileTypeFromStream } from 'file-type';
const type = await fileTypeFromStream(stream);
if (!type || !['png','jpeg'].includes(type.ext)) { res.status(415).end('bad type'); return; }

Test both sides of the limit

Assert a file under the limit lands in storage and a file over the limit returns 413 and writes nothing, so the guard is enforced end to end.

The trade-off worth naming is that assert a file under the limit lands in storage and a file over the limit returns 413 and writes nothing, so the guard is enforced end to end adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: busboy, Node streams.

FAQ

Why not just use multer?
multer buffers to disk or memory by default; for unbounded sizes you want a streaming parser like busboy that hands you a readable stream you pipe onward, so the file never sits whole in your process.
How do I abort mid-stream?
Destroy the part stream and the response when the size guard trips, and backpressure stops the client from sending more bytes.

Related Examples