Skip to content

Static Serve

The @gaman/static package allows you to serve static files (images, CSS, JS, etc.) from a directory. It includes automatic MIME type detection, ETag generation, and support for Brotli/Gzip compression.

Terminal window
npm install @gaman/static

Register the static file middleware in your bootstrap file using app.mount():

import { defineBootstrap } from 'gaman';
import { StaticServe } from '@gaman/static';
defineBootstrap(async (app) => {
// Serve files from the 'public' folder (default)
app.mount(StaticServe());
// Or with custom configuration
app.mount(StaticServe({
publicPath: './assets',
rewriteRequestPath: (p) => p.replace(/^\/static/, ''),
fallbackToIndexHTML: true, // SPA fallback
}));
app.mountServer({ http: 3431 });
});
OptionTypeDefaultDescription
publicPathstring"public"Path to the directory to serve
defaultDocumentstring"index.html"File to serve when a directory is requested
rewriteRequestPath(path: string) => stringRewrite request paths (e.g., strip prefix)
fallbackToIndexHTMLbooleanfalseFallback to index.html on 404 (SPA support)
cacheControlstringCustomize Cache-Control header
mimesRecord<string, string>Custom MIME type mappings
onFound(path, ctx) => voidCallback when a file is found
onNotFound(path, ctx) => voidCallback when a file is not found
  • Automatic Compression: Serves .br or .gz files if they exist and the browser supports it.
  • ETag & 304: Automatic ETag generation for efficient browser caching.
  • Performance: Built on top of Bun.file() for maximum speed.