Serving requests
Turning an app into a plain request handler.
package @smitejs/http
serve(app) turns a declared app into an HttpRouter: a plain (request) => Promise<response> function you can hand to any HTTP server or async handler.
What serve does
- Freezes the app into a stable, read-only structure, so dispatch never sees a half-built declaration.
- Builds path matchers from every endpoint under the app's route.
- Each request is matched by path then method, validated against the route's declared schemas, and dispatched to the endpoint's handler.
ts
const app = http.app("store");
const routes = http.router();
routes.accept("GET", "/health").handler(() => json({ ok: true }));
app.use(routes);
const router = serve(app);
const response = await router({
method: "GET",
path: "/health",
query: {},
headers: {},
cookies: {},
params: {},
body: undefined,
});ts
{ status: 200, body: { ok: true } }tested in packages/http/src/index.test.ts:368
Dispatch rules
- The route's
inputschemas are validated per request; failure yields400. - An unmatched path or method yields
404with{ error: "Not Found" }. - An endpoint with no handler also yields
404. ANYendpoints match every method; otherwise the method must match exactly.
Bringing it to the wire
HttpRouter is intentionally free of any Node or platform dependency — it takes an HttpRequest object and returns an HttpResponse. A tiny adapter (an acceptable http.createServer handler, an edge function, a test harness) supplies the transport. That adapter is where the real server lives; Smite stops at the I/O boundary.