---
title: Serving Routes
description: 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](/docs/server/framework-integration) for the last
two.

## Create an App

**Bun, Deno, Workers**

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

export default createApp(router);
```

**Node**

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

createServer(createApp(router)).listen(3000);
```

- [Errors](/docs/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:

```ts
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()`.

:::warning
The Node app reads request bodies whole, with no size limit. Limit them in
front of it, such as in a proxy.
:::

## Options

```ts
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`:

```ts
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](/docs/route-builder#stacking-calls).
