StaticRoute

The StaticRoute class defines a route that serves static files with automatic registration to the global router. It accepts a path and a file definition (either a plain path string or an object with stream: true for large files for download). An optional custom handler can intercept file content before sending to modify response or transform content.

Contents
  1. Usage
  2. Constructor Parameters
  3. Properties

Usage

Routes can be instantiated directly with new. The constructor automatically registers the route to the global router store.

Simple file serve

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

function addr(...path: string[]) {
	return X.Config.resolvePath(X.Config.cwd(), ...path);
}

// GET /style serves assets/style.css
new C.StaticRoute("/style", addr("assets", "style.css"));

Streaming large files

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

// Stream video directly from disk
new C.StaticRoute("/video", {
	filePath: addr("assets", "video.mp4"),
	stream: true,
});

Custom handler

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

// Modify response and content before sending
new C.StaticRoute("/doc", "assets/doc.txt", (c, content) => {
	c.res.headers.set("x-custom", "value");
	return content.replaceAll("hello", "world");
});

Constructor Parameters

path

E extends string

The URL endpoint path. Always uses GET method.

definition

type StaticRouteDefinition =
	// just the file path, doesn't stream
	| string
	| {
			filePath: string;
			stream: true;
			// defaults to attachment
			disposition?: "attachment" | "inline";
	  };

The file definition. If a string is provided, serves the file normally. Use the object form with stream: true for large files to stream directly from disk without loading into memory.

Value Behavior
"style.css" Standard file response
{ filePath: "assets/video.mp4", stream: true } Streams file from disk

`handler` (optional)

(context: Context<B, S, P, R>, content: string) => MaybePromise<R>

Optional custom handler to intercept file content before sending. Receives the context and file content as string. Use c.res.headers to modify response headers. Must return string or a CResponse.

(c, content) => {
	c.res.headers.set("x-custom", "value");
	return content; // or return new C.Response(...)
};

model (optional)

RouteModel<B, S, P, R>

Optional validation model for search params and response. See Model. You can pass generics if you don't want to bother with validation but still typecast your data: StaticRoute<B, S, P, E>

// type Schema is any standard schema library validator.
type RouteModel<B = unknown, S = unknown, P = unknown, R = unknown> = {
	response?: Schema<R>;
	body?: Schema<B>;
	search?: Schema<S>;
	params?: Schema<P>;
};

Properties

All constructor options are stored as readonly properties after resolve methods:

Property Type Description
id string Unique route identifier ({method}:{endpoint})
method Method Fixed to Method.GET
endpoint E Resolved path
handler Func<[Context<B, S, P, R>, string], MaybePromise<R>> The route handler function (file serve or custom)
model RouteModel | undefined Validation model if provided
variant RouteVariant.static Fixed to static for this class