@youneed/server-middleware-static
v0.1.0
Published
@youneed/server middleware: serve static files from disk with HTTP Range, ETag, and conditional-request support.
Downloads
22
Readme
@youneed/server-middleware-static
Serve static files from disk, with HTTP Range
support (so browsers can seek <video>/<audio> and resume downloads),
ETag +
Last-Modified validators, and conditional 304 responses.
import { Application, Response } from "@youneed/server";
import { staticFiles } from "@youneed/server-middleware-static";
const app = Application()
.use(staticFiles("public", { cacheControl: "public, max-age=3600" }))
.get("/api/health", () => Response.json({ ok: true }));Mount it before your routes. Any miss — a non-GET/HEAD method, a path that
isn't a file, a directory with no index, or a path-traversal attempt — falls
through via next(), so your routes still see the request.
API
staticFiles(root, opts?)— middleware serving files underroot.- Only handles
GET/HEAD; other methods fall through. - Resolves the request path under
rootand rejects path traversal (..). - Directories serve
opts.index(default"index.html"). - Sets
Content-Typefrom the extension,ETag,Last-Modified, and alwaysAccept-Ranges: bytes. If-None-Matchmatching theETag→304 Not Modified(no body).Range: bytes=START-END→206 Partial ContentwithContent-Rangeand only the requested bytes (supportsbytes=START-andbytes=-SUFFIX); an out-of-bounds range →416withContent-Range: bytes */SIZE.
Options:
index— file served for a directory (default"index.html").cacheControl— value for theCache-Controlheader (set only when given).
- Only handles
