# Don't Serve uploads/ Blindly: Safe File Serving in Express

Many Express tutorials finish a file-upload feature with one line:

```js
// ❌ exposes everything in the folder — including private files or stray HTML
app.use(express.static("uploads"));
```

That single line works until a user uploads a private invoice next to public avatars, or a stray `.env` or `.html` file ends up in the served directory. Storing uploaded files and serving them safely are two separate decisions: **public assets** can be served via `express.static` with strict options, while **private files** belong behind an authenticated route using `res.download`.

Every response and header below comes from a real run on **Node.js 24.22.1** and **Express 5.3.0**.

## Where uploaded files should live: folder-based structure

Keep uploaded files **outside** your source folder (`src/`), split public files from private ones, and add `uploads/` to `.gitignore` so user files never enter Git history:

```text
my-app/
├── src/
│   └── server.js
├── uploads/
│   ├── public/
│   │   └── avatars/
│   │       └── u42.png
│   └── private/
│       └── invoice-101.txt
└── .gitignore   # includes uploads/
```

<!-- mermaid source: arrows are written as --&gt; so this comment stays hidden; replace &gt; with > before reusing
flowchart TD
    Root["my-app/"] --&gt; Src["src/server.js"]
    Root --&gt; Git[".gitignore<br/>(ignores uploads/)"]
    Root --&gt; Up["uploads/"]
    Up --&gt; Pub["public/avatars/<br/>served by express.static"]
    Up --&gt; Priv["private/<br/>authenticated res.download only"]
-->
![Upload storage folder structure: project root contains src/server.js, .gitignore (ignoring uploads/), and uploads/ split into uploads/public/avatars/ (served via express.static) and uploads/private/ (served only via authenticated res.download)](https://mermaid.ink/img/pako:eNpljjFrw0AMRv-K0NRCbe-mZCp0LW0zWR7kOyW5YvsOnezUhPz3Yoe2QzbB03t8F3TRC9Z46OPZnVgNPl9oBAB4j9EawmEpOKWKsIWi2MGHuoYwq6uy6CxafmXC9t_Ynl7DapbHYOE4RpXnTqvdw-3OMKU-ss_V4724Tw3hL__D-7TBt6lrCNPU9cFVPLOx5mpLb1M8dAvId1LJuczGFtxdQcO8JjTMbHJzebKTjBYcm3hQyaWP53FdAHHsF8IWn3AQHTh4rPFCaCcZhLAm9HLgqTfCK15_AGbjbUk=?type=png)

Three habits keep this structure maintainable:
- **Store only the relative key in your database** (such as `avatars/u42.png`), not a full disk path like `C:\app\uploads\...` or `http://localhost:3000/...`. Moving to a new server or cloud bucket later requires changing one prefix instead of rewriting database rows.
- **Never expose `uploads/private/` with `express.static`.** Only `uploads/public/` should ever be mapped to a public URL prefix.
- **Create the folders on startup.** Because `uploads/` is in `.gitignore`, a fresh `git clone` will not contain those folders. Run `fs.mkdirSync(publicDir, { recursive: true })` and `fs.mkdirSync(privateDir, { recursive: true })` when your server boots so the first upload never crashes with `ENOENT`.

## Local storage vs external object storage

Local disk storage is ideal while learning and for small single-server apps, but it has real limits in production. Compare the two before deciding where uploads live:

| Factor | Local disk (`uploads/`) | External object storage (S3, GCS, R2) |
|---|---|---|
| Setup effort | Zero extra services; create a folder | Requires bucket, IAM keys, and SDK |
| Survives redeploys | No on ephemeral containers / PaaS | Yes (independent of app servers) |
| Multiple app servers | Breaks (files on Server A 404 on Server B) | Works across any number of servers |
| CDN & bandwidth | Express serves every byte itself | Offloaded to CDN / object storage |
| Best fit | Local dev, single-VM internal tools | Production web apps at scale |

Start on local disk with clean relative paths (`avatars/u42.png`) so switching to external object storage later does not require redesigning your database schema.

## How static file serving works in Express

**Static file serving** means mapping a URL path straight to a directory on disk without writing a separate `app.get()` route for every file. Express ships `express.static(rootDir, options)` for this job.

When a client requests `GET /files/avatars/u42.png`, Express strips the `/files` mount prefix, looks up `avatars/u42.png` inside `uploads/public/`, sets `Content-Type` from the file extension, and streams the bytes back. If no matching file exists, it calls `next()` so your normal routes or `404` handler can respond.

<!-- mermaid source: arrows are written as --&gt; so this comment stays hidden; replace &gt; with > before reusing
flowchart LR
    Req["GET /files/avatars/u42.png"] --&gt; MW["express.static(publicDir)<br/>dotfiles: ignore, index: false"]
    MW --&gt; Check{"File exists<br/>& not dotfile?"}
    Check -- "Yes" --&gt; Stream["Set Content-Type + nosniff<br/>200 Stream Bytes"]
    Check -- "No" --&gt; Next["Call next()<br/>404 JSON Handler"]
-->
![Static file serving flow in Express: client requests GET /files/avatars/u42.png, Express strips /files and runs express.static on uploads/public/ with dotfiles ignored and index disabled; if found it sets Content-Type and nosniff and streams the file with 200, otherwise it calls next() to return 404 JSON](https://mermaid.ink/img/pako:eNplz09rwkAQBfCvMsyhWGpMEE-hWKj9R6kWVJCSyWFNJmbpZjfdHduI-N1Lo7fe3_s93hELVzKmWBn3U9TKC7wtyQIALPkrI3x-XENcacMhVt9KlA_xfjIetXZHmEMUTWG-yQi5az2HMAqiRBeDdr81unjQ_vp26-Np6aQnUtA76zwPQduSuxQqZQIT5ufF-aYHZzUXn0fCJ20YuNNBQq9cgXUCF-uO8HRu9XGIIiD84EDYGyvxrJqMcMUCM2eFrUTrQ8twA9YFq6uqN8dJcsnC_UH-6vk_deEu6II7yQhnyhiw3Mng_G6STOB19b6AF2VLw54wxyE27BulS0zxSCg1N0yYEpZcqb0RwhOefgFAPXn-?type=png)

Mount `express.static` on a dedicated prefix (`/files`, not `/`) and pass three protective options:

```js
// src/server.js (Node.js 24.22.1, Express 5.3.0)
const path = require("path");
const express = require("express");

const app = express();
const publicDir = path.join(__dirname, "..", "uploads", "public");
const privateDir = path.join(__dirname, "..", "uploads", "private");

app.use(
  "/files",
  express.static(publicDir, {
    dotfiles: "ignore", // treat hidden files like .env as 404
    index: false,       // do not auto-serve index.html from upload folders
    setHeaders: (res) => {
      res.setHeader("X-Content-Type-Options", "nosniff");
    },
  })
);
```

- **`dotfiles: "ignore"`** prevents hidden files (like `.env` or `.htaccess`) from being served even if one accidentally lands in the folder.
- **`index: false`** stops Express from automatically rendering an `index.html` if someone requests a directory URL.
- **`X-Content-Type-Options: nosniff`** tells browsers not to guess a different MIME type (for example, executing a mislabelled file as HTML or JavaScript).
- **Optional caching (`maxAge`):** when your upload filenames include a unique UUID (so files are never overwritten in place), passing `maxAge: "1d"` to `express.static` lets browsers reuse cached avatars instead of downloading them on every page load.

## Accessing uploaded files via URL (real Express 5.3.0 output)

With `u42.png` inside `uploads/public/avatars/` and a decoy `.env` file inside `uploads/public/`, three real requests returned:

```text
GET /files/avatars/u42.png -> 200 | type=image/png | nosniff=nosniff | body=fake-png-bytes
GET /files/.env -> 404 | type=application/json | body={"error":"Not found"}
GET /files/avatars/missing.png -> 404 | type=application/json | body={"error":"Not found"}
```

The avatar streamed back with `image/png` and `nosniff`, while both `.env` and the missing file fell through cleanly to our JSON `404` handler.

## Controlled downloads for private files

For private files (invoices, resumes, user exports), **do not** use `express.static`. Put them behind a route where you can check authentication first, validate the filename against path traversal (`../`), and send the file with `res.download` and `{ root: privateDir }`:

```js
// Add authentication middleware before this handler in real apps
app.get("/download/:name", (req, res) => {
  const safeName = path.basename(req.params.name);
  if (safeName !== req.params.name || !/^[a-zA-Z0-9._-]+$/.test(safeName)) {
    return res.status(400).json({ error: "Invalid filename" });
  }

  const fullPath = path.resolve(privateDir, safeName);
  if (!fullPath.startsWith(privateDir + path.sep)) {
    return res.status(400).json({ error: "Invalid path" });
  }

  res.setHeader("X-Content-Type-Options", "nosniff");
  res.download(safeName, safeName, { root: privateDir }, (err) => {
    if (err && !res.headersSent) {
      res.status(err.statusCode || 404).json({ error: "File not found" });
    }
  });
});

app.use((req, res) => res.status(404).json({ error: "Not found" }));
app.listen(3000, () => console.log("Listening on http://127.0.0.1:3000"));
```

To reproduce the responses in this post, run `node setup.js` once to create the sample files, start the server with `node src/server.js`, then run `node client.js` in a second terminal. The "PNG" is only the text `fake-png-bytes`, which is enough to show the headers:

```js
// setup.js: creates the sample files used in the examples (run once from my-app/)
const fs = require("fs");

fs.mkdirSync("uploads/public/avatars", { recursive: true });
fs.mkdirSync("uploads/private", { recursive: true });
fs.writeFileSync("uploads/public/avatars/u42.png", "fake-png-bytes"); // not a real PNG, just bytes
fs.writeFileSync("uploads/public/.env", "SECRET=1");                  // decoy dotfile
fs.writeFileSync("uploads/private/invoice-101.txt", "Invoice #101: Paid");
```

```js
// client.js: run while src/server.js is listening
const base = "http://127.0.0.1:3000";

async function show(path) {
  const res = await fetch(base + path);
  const parts = [`GET ${path} -> ${res.status}`, `type=${(res.headers.get("content-type") || "").split(";")[0]}`];
  const nosniff = res.headers.get("x-content-type-options");
  if (nosniff) parts.push(`nosniff=${nosniff}`);
  const disp = res.headers.get("content-disposition");
  if (disp) parts.push(`disp=${disp}`);
  parts.push(`body=${await res.text()}`);
  console.log(parts.join(" | "));
}

(async () => {
  await show("/files/avatars/u42.png");
  await show("/files/.env");
  await show("/files/avatars/missing.png");
  await show("/download/invoice-101.txt");
  await show("/download/..%2Fsecret.txt");
})();
```

Real responses for a valid private file and a `../` path-traversal attempt:

```text
GET /download/invoice-101.txt -> 200 | type=text/plain | nosniff=nosniff | disp=attachment; filename=invoice-101.txt | body=Invoice #101: Paid
GET /download/..%2Fsecret.txt -> 400 | type=application/json | body={"error":"Invalid filename"}
```

Passing `{ root: privateDir }` to `res.download` does two things: it confines file resolution inside `privateDir`, and it avoids a subtle Express 5 `send` trap where an absolute path containing a dot-prefixed ancestor directory (such as a `.config` or `.cache` parent folder) gets rejected as a dotfile.

## Security considerations for serving uploads

Before exposing uploaded files over HTTP, verify five rules:
1. **Separate public and private folders.** Only mount `express.static` on the public directory.
2. **Send `X-Content-Type-Options: nosniff`** on every served upload so browsers obey your declared `Content-Type`.
3. **Never serve user-uploaded `.html`, `.js`, or `.svg` files inline** from your app's primary domain — SVG files can embed `<script>` tags and trigger stored XSS. Force downloads with `Content-Disposition: attachment` or serve user content from a separate sandbox domain.
4. **Block path traversal on dynamic file routes.** Combine `path.basename`, a strict character allowlist, and `{ root: privateDir }` so `..%2F` cannot escape the upload folder.
5. **Check authorization on private routes.** Never rely on "hard-to-guess filenames" as access control for sensitive documents.

## When NOT to serve uploaded files through Express

Express is fine for local development and modest traffic, but Node.js should not spend CPU and bandwidth streaming large videos, high-traffic image galleries, or gigabyte backups on every request. Once traffic grows, put a reverse proxy (Nginx) or a CDN in front of your files, or serve them directly from object storage (S3, GCS, R2) using public CDN URLs or short-lived pre-signed URLs.

## Where this leaves you

Keep uploads outside `src/`, split `uploads/public/` from `uploads/private/`, serve public assets with `express.static` (`dotfiles: "ignore"`, `index: false`, `nosniff`), and guard private files behind authenticated `res.download` routes with strict filename validation.

One honest limit: this walkthrough stays on local disk without an authentication middleware wired into `/download/:name` and without cloud pre-signed URLs or full CDN cache-invalidation strategies. Have you ever had `express.static` accidentally expose files you didn't intend to make public?

