Route Metadata
ctx.route identifies the route that handled a request.
interface RouteMeta {
path: string; // the requested pathname, e.g. "/api/users/123"
pattern: string; // the route pattern, e.g. "/api/users/:id" (includes apiPrefix)
}
Available on every matched route
ctx.route is set for every matched route. On Bun, serve() registers every route (static, :param, wildcard) as a per-method native Bun route; the fetch handler path (Cloudflare, Deno, Vercel, Node) uses a fast radix matcher. For a static route, path and pattern are the same value.
Uses
- Logging which pattern served a request.
- Building links or HAL-style responses from the current pattern.
- Instrumentation and analytics keyed by route pattern.
api/products/route.ts
import type { BurgerContext } from "burger-api";
export async function GET(ctx: BurgerContext) {
// pattern includes the API prefix, e.g. "/api/products"
// The type is RouteMeta | undefined; in a matched handler it is always set.
console.log(`served by ${ctx.route?.pattern}`);
return Response.json({ ok: true });
}
Route convention files
ctx.route comes from the route directory. Each route directory is self-contained, with optional sibling convention files:
| File | Role |
|---|---|
route.ts | export async function GET(ctx: BurgerContext) returns a Response |
schema.ts | export const GET = { body, query, ... } |
hooks.ts | Route hooks |
openapi.ts | export const GET = { summary, tags, ... } |
config.ts | Route options (auth, cache, timeout, ...) |
Per-method named exports (GET, POST, ...) are used on route.ts, schema.ts, and openapi.ts. See File-Based Routing.