Skip to content

Inline route handlers

Register one-off HTTP routes with host.http.get and HttpRouteOptions - inject maps, descriptors, and isolated routes.

AI generated, pending review Updated 10 days ago · Flare 0.3

Register a route with host.http.<method>(path, [options], handler). Supported methods are GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS.

The handler is an HttpRouteHandler: (ctx, scope) => HandlerResult | Promise<HandlerResult>. Omit either parameter when you do not need it.

import { FlareHost, FlareResponse } from "@flare-ts/core";
import { node } from "@flare-ts/core/node";
const host = new FlareHost(node);
host.http.get("/ping", () => new FlareResponse(200, { ok: true }));
host.http.get("/users/:id", (ctx) => {
return new FlareResponse(200, { id: ctx.req.rawRouteParams["id"] });
});
const app = host.build();
app.run();

The second argument is an HttpHandlerScope with reserved keys and your inject map entries:

MemberPurpose
scope.inputParsed route, query, and body when the route options carry descriptor fields or a contract entry
scope.config(Token)Resolved config section
scope.<name>Services from inject: { name: Token }

See Named inject map. Do not use input or config as inject map keys.

import { FlareHost, FlareResponse, FlareService } from "@flare-ts/core";
import { node } from "@flare-ts/core/node";
import { int } from "@flare-ts/lib/schema";
class DbService extends FlareService {
public static override deps = [];
findById(id: number) {
return { id };
}
}
const host = new FlareHost(node);
host.scoped(DbService);
host.http.get(
"/users/:id",
{ route: { id: int }, inject: { db: DbService } },
(_ctx, scope) => new FlareResponse(200, scope.db.findById(scope.input.route.id)),
);
const app = host.build();
app.run();
  • Paths must start with /.
  • Paths must not end with /, except the lone path / is allowed.
  • Paths must not contain empty segments (//); registration throws immediately.
  • Registering the same HTTP method twice at the same path throws at registration time.

You can register different methods at the same path. Flare merges them into one synthetic controller.

OptionPurpose
routeCoerce named :id segments (int, str)
queryCoerce query-string fields
bodyJSON schema, model(), or stream body marker
responsePer-status response serializers
maxBodyBytesPer-route body size cap
signedCookiesOpt into build-time check for cookies.secret
contractBranded httpContract entry (use this or inline fields, not both)
injectNamed service map: { name: Token }
stateState tokens the handler reads via ctx.state
isolatedWhen true, skip global and group middleware
nameOptional display name for logs and error targets

Without descriptor fields, matched path segments are plain strings on ctx.req.rawRouteParams. Query keys are on ctx.req.rawQueryParams (a URLSearchParams).

Set { isolated: true } when a route must bypass global and group middleware (for example a health check that must not run auth):

host.http.get("/health", { isolated: true }, () => new FlareResponse(200, { ok: true }));

Inside host.http.group, use g.get, g.post, and the same overloads. Paths are prefixed with the group prefix. See Route groups and versioning.