Skip to content
rest-rpc
Esc
↑↓navigate↵open⌘Jpreview
On this page

Serving Routes

Serve routes with any framework or none, on Fetch runtimes or Node.

Routes don’t depend on how they’re served, so any framework or none works, and switching later doesn’t change them:

  • createApp: no framework. Covered on this page.
  • Catch-all handler: a framework serves the requests under one of its routes, such as /api/*.
  • Route handlers: each route is registered on the framework’s router, so its middleware can apply per route.

See Framework Integration for the last two.

Create an App

import { createApp } from "@rest-rpc/server";
import { router } from "./router.ts";

export default createApp(router);
import { createServer } from "node:http";
import { createApp } from "@rest-rpc/server/node";
import { router } from "./router.ts";

createServer(createApp(router)).listen(3000);
  • Errors are sent as error responses, and anything else thrown as INTERNAL_SERVER_ERROR.
  • Requests that match no route get NOT_FOUND.
  • On Node, a response that already started, such as a stream that fails, is closed instead.

Bun

The app is { fetch, routes }, which Bun serves as a default export, using its own router for routes. To set other Bun options, spread it:

const app = createApp(router);

Bun.serve({
	...app,
	port: 8080,
	routes: { ...app.routes, "/health": new Response("ok") },
});

Node

createApp from @rest-rpc/server/node returns a request listener for http.createServer() or https.createServer().

Options

type CreateAppOptions = {
	/**
	 * The path the routes are served under, such as `/api`. Clients take the
	 * same `mountPath`.
	 */
	mountPath?: `/${string}`;
	/**
	 * The initial context the routes require, or a function that returns it
	 * for each request. Required when any route declares initial context.
	 */
	context?:
		| TContext
		| ((request: Request, ...args: TArgs) => TContext | Promise<TContext>);
	/** Functions around every request. See Interceptors. */
	interceptors?: readonly Interceptor[];
	/** Codecs for other body formats. See Content Types and Codecs. */
	bodyCodecs?: readonly BodyCodec[];
	/** Skips request validation. The types stay as if the schemas ran. */
	disableRequestValidation?: boolean;
	/** Skips response validation. The types stay as if the schemas ran. */
	disableResponseValidation?: boolean;
	/** Keepalive and data format of event streams. See Server-Sent Events. */
	sse?: SseOptions;
};

On Node, the context function receives the IncomingMessage. On Fetch runtimes, it also receives what the runtime passes after the request, such as Cloudflare Workers’ env:

export default createApp(router, {
	context: (request, env: Env) => ({ db: env.DB }),
}) satisfies ExportedHandler<Env>;

Paths

  • Path params are written :name, and their values are percent-decoded.
  • Static segments match before params, so /posts/latest wins over /posts/:id.
  • One trailing slash is ignored.

mountPath is removed before matching, and isn’t part of the routes. A path that clients and OpenAPI should see on every route, such as /v1, belongs in the routes as a prefix.

Was this page helpful?