Packages / Serve Static

Serve Static

Static file server with Range, conditional GET, charset, dotfile guard, and SPA fallback.

@miiajs/serve-static serves files from disk under a URL prefix. The package speaks full HTTP semantics: weak ETag and Last-Modified for cache revalidation, Range requests for resumable downloads and <video>/<audio> seeking, HEAD without body, and a SPA fallback gated by Accept and path extension. Default-deny dotfile policy keeps .env, .git/HEAD, and .well-known/... from leaking.

Installation

bun add @miiajs/serve-static

On Deno, use 2.5.0 or newer: older releases crash the process after a file is sent (a bug in Deno's Readable.toWeb, fixed in 2.5.0).

Setup

Mount a directory under a URL prefix:

typescript
import { Miia } from '@miiajs/core'
import { serveStatic } from '@miiajs/serve-static'

const app = new Miia().register(AppModule)

serveStatic(app, '/static', './public')

This serves all files from ./public under the /static URL prefix:

GET /static/style.css  ->  ./public/style.css
GET /static/app.js     ->  ./public/app.js
GET /static/logo.png   ->  ./public/logo.png

Configuration options

typescript
serveStatic(app, '/assets', './dist', {
  index: 'index.html',
  maxAge: 3600,
  etag: true,
  dotfiles: false,
  fallback: 'index.html',
  skipGlobalPrefix: true,
})

The first six options are ServeStaticOptions, shared with createStaticHandler(). skipGlobalPrefix is about where the route is mounted, which only serveStatic() decides, so it lives on ServeStaticMountOptions - the type serveStatic() takes, which extends ServeStaticOptions with that one flag.

OptionTypeDefaultDescription
indexstring | false'index.html'Index file for directory requests - the mount root and any subdirectory that has one. A directory URL without the trailing slash redirects (301) to the slashed one first. Set to false to disable.
maxAgenumber0Cache-Control: max-age value in seconds. Applied to 200 and 304 responses.
getMimeType(path: string) => stringbuilt-in mapCustom MIME type resolver. Result is automatically suffixed with ; charset=utf-8 for text-like types unless it already specifies a charset.
etagbooleantrueSend ETag and Last-Modified and honor If-None-Match / If-Modified-Since. Disable to skip conditional GET.
dotfilesbooleanfalseWhen false, any URL with a path segment starting with . returns 404 (e.g. .env, .git/HEAD, .well-known/...).
fallbackstring | falsefalsePath inside root to serve when the requested file is missing and the request looks like a SPA navigation. Typically 'index.html'.
skipGlobalPrefixbooleanfalseserveStatic() only. Keep the mount at prefix when the app runs under a global prefix.

Usage

A typical SPA setup - serve built assets, fall back to index.html for client-side routes, cache hashed bundles aggressively:

typescript
import { Miia } from '@miiajs/core'
import { serveStatic } from '@miiajs/serve-static'

const app = new Miia().register(AppModule)

// Hashed assets - long-lived cache + SPA fallback for deep links.
serveStatic(app, '/', './dist', {
  maxAge: 60 * 60 * 24 * 365,
  fallback: 'index.html',
})

await app.listen(3000)

GET /about resolves to dist/index.html; GET /assets/app-abc123.js returns the bundle with a year-long cache; GET /favicon.ico returns the icon. Refresh on /about keeps client-side routing intact.

HTTP semantics

Conditional GET (304 Not Modified)

Each response carries a weak ETag of the form W/"<size>-<mtime>" and a Last-Modified header. Browsers and CDNs revalidate on subsequent requests:

> GET /assets/app.js
< 200 OK
< ETag: W/"4ad-194a8c4b800"
< Last-Modified: Mon, 12 May 2025 10:00:00 GMT

> GET /assets/app.js
> If-None-Match: W/"4ad-194a8c4b800"
< 304 Not Modified

If-None-Match takes precedence over If-Modified-Since per RFC 7232. Disable both with etag: false.

Range requests (206 Partial Content)

Range: bytes=0-99, bytes=900-, and suffix form bytes=-50 are supported. The server replies 206 with Content-Range: bytes start-end/total and streams only the requested slice. Out-of-range requests return 416 with Content-Range: bytes */total. Multi-range requests fall back to a full 200.

If-Range is honored against either the ETag or the Last-Modified value - if it doesn't match, the full file is served (200) so the client knows to discard its partial cache.

HEAD requests reuse the GET pipeline (status code, all headers, including Content-Length and Content-Range for ranged HEAD) but return no body.

Charset

Text-like MIME types (text/*, application/javascript, application/json, application/xml, image/svg+xml) are returned with ; charset=utf-8 appended automatically. Binary types (images, fonts, archives) are not modified. If a custom getMimeType already specifies a charset, it's left alone.

SPA fallback

Single-page applications need any unmatched URL to resolve to index.html so client-side routing can take over after a page reload:

typescript
serveStatic(app, '/', './dist', {
  fallback: 'index.html',
})

The fallback only triggers when:

  • the request is GET or HEAD
  • Accept includes text/html or */* (so fetch('/api/users') with Accept: application/json still 404s correctly)
  • the path has no extension or ends with .html/.htm (so a missing /assets/missing.png 404s instead of returning HTML pretending to be an image)

The fallback path is served fresh each time with its own ETag, Last-Modified, and Content-Type.

A root mount is a wildcard, and the router falls back to it whenever no more specific route matches - an unknown API URL included. If the API shares the origin, close it with a catch-all, as shown in SPA in front of a prefixed API.

SPA in front of a prefixed API

An app under a global prefix prefixes the static mount too, which pushes a root-mounted SPA to /api/* where the browser never looks for it. skipGlobalPrefix pins the mount to the URL you wrote:

typescript
const app = new Miia({ globalPrefix: '/api' }).register(AppModule)

serveStatic(app, '/', './dist', {
  fallback: 'index.html',
  skipGlobalPrefix: true,
})

GET / and GET /about now serve the SPA, while @Controller('/users') keeps answering at /api/users - a route under /api is more specific than the root wildcard, so it wins.

A URL under /api that no route matches is another story. The router backs up to the next option, the root wildcard, and the fallback serves index.html to any request that accepts text/html or */*. fetch(), curl and axios send */* by default, so a mistyped API path answers 200 with HTML instead of 404. Close the prefix with a catch-all that throws:

typescript
import { Miia, NotFoundException } from '@miiajs/core'
import { serveStatic } from '@miiajs/serve-static'

const app = new Miia({ globalPrefix: '/api' }).register(AppModule)

// Lands under the prefix as GET /api/*.
app.addRoute(
  'GET',
  '/*',
  () => {
    throw new NotFoundException()
  },
  { skipGlobalGuards: true },
)

serveStatic(app, '/', './dist', {
  fallback: 'index.html',
  skipGlobalPrefix: true,
})

GET /api and GET /api/anything-unknown now answer 404, HEAD too through the GET fallback, and every real route under /api still wins over the catch-all, since a wildcard is the last option at its segment. skipGlobalGuards keeps the answer a plain 404, the way the router answers a URL it cannot match: without it a global AuthGuard from app.useGuard() would answer 401 first. The message is Not Found rather than the router's Cannot GET /api/...; pass one to NotFoundException if clients read it. Other methods need nothing: the static mount registers GET only, so POST /api/unknown is a 404 anyway. Without a global prefix the recipe is the same, with the path written out: app.addRoute('GET', '/api/*', ...).

Dotfiles policy

By default, any URL segment starting with . returns 404, so a stray .env next to your public/ directory or a .git/HEAD won't leak:

GET /static/.env             ->  404
GET /static/.well-known/x    ->  404
GET /static/foo/.git/HEAD    ->  404

Set dotfiles: true to allow them (useful for .well-known/acme-challenge/... for ACME/HTTPS):

typescript
serveStatic(app, '/.well-known', './public/.well-known', { dotfiles: true })

Custom MIME types

Override or extend MIME type detection with a custom getMimeType function:

typescript
import { serveStatic, getMimeType } from '@miiajs/serve-static'

serveStatic(app, '/assets', './public', {
  getMimeType: (path) => {
    if (path.endsWith('.glb')) return 'model/gltf-binary'
    if (path.endsWith('.m3u8')) return 'application/vnd.apple.mpegurl'
    return getMimeType(path)
  }
})

The built-in getMimeType is exported for use as a fallback.

Advanced: raw handler

For custom routing, use createStaticHandler directly:

typescript
import { createStaticHandler } from '@miiajs/serve-static'

app.addRoute('GET', '/custom/*', createStaticHandler('./public', { maxAge: 3600 }))

A global prefix applies to this route too (/api/custom/*). createStaticHandler takes ServeStaticOptions, which has no skipGlobalPrefix - the handler does not own its mount, so pinning the URL is addRoute's job: app.addRoute('GET', '/custom/*', handler, { skipGlobalPrefix: true }) - see Routing → Routes outside the prefix.

Security

  • Percent-decoding first - the path is decoded before any check, so hello%20world.txt serves hello world.txt and an encoded ..%2f or %2e segment meets the same guards as a literal one. Malformed encoding or an encoded NUL answers 400.
  • Lexical traversal protection - paths normalized with .. segments outside root are rejected before any filesystem call.
  • Symlink escape protection - realpath() resolves the target and re-checks it stays under root. A symlink inside public/ pointing outside is treated as 404.
  • Dotfile guard - on by default, see above.
  • Fallback path validation - if you set fallback: '.something' while dotfiles is disabled, createStaticHandler throws at startup rather than serving a guard-bypassing fallback.

Supported MIME types

CategoryExtensions
Text.html, .css, .txt, .csv, .xml, .yaml, .yml
JavaScript.js, .mjs, .cjs, .wasm
Data.json, .map, .pdf
Images.svg, .png, .jpg, .jpeg, .gif, .webp, .avif, .ico
Fonts.woff, .woff2, .ttf, .otf, .eot
Audio/Video.mp3, .ogg, .wav, .mp4, .webm
Archives.zip, .gz, .tar

Unknown extensions default to application/octet-stream.

API Reference

serveStatic(app, prefix, root, options?)

Registers a wildcard GET ${prefix}/* route on app. Convenience wrapper for the common case.

typescript
serveStatic(app: Miia, prefix: string, root: string, options?: ServeStaticMountOptions): void

createStaticHandler(root, options?)

Returns a RequestContext-handler. Use when you need to mount the handler under a custom route, behind guards, or composed with middleware.

typescript
createStaticHandler(root: string, options?: ServeStaticOptions): (ctx: RequestContext) => Promise<Response>

getMimeType(path)

Built-in MIME resolver. Exported for use as a fallback inside a custom getMimeType.

typescript
getMimeType(path: string): string

Testing

Use a Miia instance directly - mount serveStatic against a temp directory, then drive it with app.fetch():

typescript
import { mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Miia } from '@miiajs/core'
import { serveStatic } from '@miiajs/serve-static'

const root = mkdtempSync(join(tmpdir(), 'static-'))
writeFileSync(join(root, 'hello.txt'), 'hello world')

const app = new Miia({ logger: false })
serveStatic(app, '/static', root)
await app.init()

const res = await app.fetch(new Request('http://localhost/static/hello.txt'))
expect(res.status).toBe(200)
expect(await res.text()).toBe('hello world')

await app.destroy()

The same pattern works for asserting ETag round-trips, Range slicing, dotfile blocks, and SPA fallback - just toggle the relevant option and inspect headers on the response.

Exports

typescript
import {
  serveStatic,
  createStaticHandler,
  getMimeType,
  type ServeStaticOptions,
  type ServeStaticMountOptions,
} from '@miiajs/serve-static'

See also

Edit this page on GitHub