Skip to Content
Senri
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 ETag and Last-Modified, and answers 304 when the file has not changed
  • Content-Type comes from the extension (application/octet-stream if unknown)
  • For a folder, it serves the index.html in it (change it with index)
  • 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, .git and 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, ...).

On this page