Requests and responses
Static files
Serve the files in a folder with serveStatic() from @senrijs/senri/static
import { } from "@senrijs/senri";
import { } from "@senrijs/senri/static";
const = ();
.(({ : "./public", : "/static", : "public, max-age=3600" }));
// GET /static/app.js → ./public/app.js- It answers GET and HEAD. When no file matches, it calls
next(), so later routes still run - It sends a weak
ETagandLast-Modified, and answers 304 when the file has not changed Content-Typecomes from the extension (application/octet-streamif unknown)- For a folder, it serves the
index.htmlin it (change it withindex) - It works on Bun, Deno and Node.js. Cloudflare Workers cannot read files, so use Static Assets there
Options
Prop
Type
What it protects
- It never serves files outside
root, even through symbolic links. It checks the opened file itself, so a link swapped in while a request is handled does not lead outside either - Dotfiles (
.env,.gitand so on) are not served by default - On Windows and macOS, a path must spell each name exactly as on disk (case, short 8.3 names and trailing
.do not match)
Keep files that need protection outside root, rather than guarding part of root with app.use(path, ...).