StaticRoute

Serving a file read once at startup, optionally through a handler.

StaticRoute is the RouteVariant.static member of the RouteBase family. Unlike FileRoute, which reads its file per request, this one reads at construction and holds the bytes — so the file is served from memory, and changes on disk are not picked up until the process restarts.

The optional callback receives the file's contents as text, which is what makes this the route for templating: an HTML shell can have values injected into it before being sent.

import { StaticRoute } from "@ozanarslan/corpus";

new StaticRoute("GET /about", "./pages/about.html");
new StaticRoute("GET /", "./pages/index.html", (c, html) => html.replace("{{title}}", title));
Contents
  1. StaticRoute

StaticRoute

class

class StaticRoute<B = unknown, S = unknown, P = unknown, E extends string = string, >extends RouteBase<B, S, P, StaticRouteRes, E>

Serves a file held in memory, optionally passing it through a callback first.

The file is read once during construction and kept as bytes, so every request is answered without touching the filesystem. A file that does not exist at that point is not an error — the route registers, and requests reach StaticRoute.onFileNotFound instead.

Type parameters

StaticRoute.constructor()

constructor();
constructor(address: RouteAddress<E>, filePath: string, callback?: StaticRouteCallback<B, S, P>, config?: StaticRouteConfig<B, S, P>, );
constructor(address?: RouteAddress<E>, filePath?: string, callback?: StaticRouteCallback<B, S, P>, config?: StaticRouteConfig<B, S, P>, )

Creates a static route for subclasses, which declare StaticRoute.endpoint and StaticRoute.file as class fields and call RouteBase.register themselves.

Creates a static route, reads its file, and registers it on the nearest App.

Parameters

StaticRoute.file

file: Nullable<XFile>;

The file this route serves. Kept for its mime type and name; the contents live in StaticRoute.bytes.

StaticRoute.bytes

bytes: Nullable<Uint8Array>;

The file's contents, read at construction. null when the file did not exist then, which is what sends requests to StaticRoute.onFileNotFound.

StaticRoute.cacheHeader

cacheHeader: string;

The Cache-Control value sent with the file, rendered from the config at construction rather than per request.

StaticRoute.callback

callback?: StaticRouteCallback<B, S, P>

Transforms the file's contents before they are sent. Absent means the bytes are sent unchanged.

StaticRoute.onFileNotFound

onFileNotFound: () => Promise<StaticRouteRes>;

Decides what to serve when the file was missing at construction. Replace it to serve a placeholder or redirect instead of throwing.

Returns — The StaticRouteRes to send instead.

Throws — Exception with Status.NOT_FOUND by default.

StaticRoute.variant

override readonly variant: RouteVariant

Marks this route as RouteVariant.static for App route compilation.

StaticRoute.method

override method: Method

The method the file is served on. Taken from the RouteAddress; defaults to Method.GET.

StaticRoute.endpoint

override endpoint!: E

The path the file is served at.

StaticRoute.config

override config?: RouteConfig<B, S, P, StaticRouteRes>

Validation schemas for the route. The caching policy is stripped out during construction, so what remains is a plain RouteConfig.

StaticRoute.handler

override handler: ContextHandler<B, S, P, StaticRouteRes>

Sends the file, or the callback's transformation of it.

The content type, caching and length headers are set from the file before the callback runs, so a callback that changes the content's length — or returns a Res of its own — should set those headers itself.

Parameters

Returns — The file's bytes, or whatever the StaticRoute.callback returned. Falls through to StaticRoute.onFileNotFound when the file was missing at construction.