# @askrjs/server/router

> Published API exports for @askrjs/server/router.

Source: [https://askrjs.com/docs/reference/api/server/router](https://askrjs.com/docs/reference/api/server/router)

Status: stable. Packages: @askrjs/server/router.

**Published packages are authoritative.** Examples may lag behind a published contract. When guidance differs, verify the exports and TypeScript declarations in your installed package, then file an issue.

## Exports

This entrypoint publishes 9 exports from the declarations shipped by @askrjs/server.

### `ApiRoute`

```ts
ApiRoute: any
```

A single registered route: a path/method pattern paired with a handler (or WebSocket upgrade handler).

- `path`: path: string;

- `method`: method?: string | readonly string[];

- `handler`: handler: Handler<RouteParams>;

- `upgrade`: upgrade?: WebSocketHandler<RouteParams>;

### `ApiRouteOptions`

```ts
ApiRouteOptions: any
```

Per-route configuration shared by {@link ApiRoute}.

- `auth`: auth?: AuthRequirement;

- `middleware`: middleware?: readonly Middleware<RouteParams>[];

- `maxRequestBytes`: maxRequestBytes?: number;

### `createRouter`

```ts
createRouter: () => Router
```

Creates an empty, mutable {@link Router} with chainable HTTP-method route builders
(`get`, `post`, `put`, `patch`, `delete`, `options`, `head`, `trace`, `connect`, `ws`) and a
`use` method for registering middleware.

### `defineRoutes`

```ts
defineRoutes: (definition: (route: RouteBuilder) => void) => ApiRoute[]
```

Builds a flat list of {@link ApiRoute}s by invoking `definition` with a {@link RouteBuilder}.
Useful for defining a set of routes without a full {@link Router} (e.g. to compose into one).

### `Handler`

```ts
Handler: {
  bivarianceHack(context: ServerContext<RouteParams>): Response | Promise<Response>;
}["bivarianceHack"]
```

A route handler function that produces a response for a given {@link ServerContext}.

### `Middleware`

```ts
Middleware: {
  bivarianceHack(context: ServerContext<RouteParams>, next: Next): Response | Promise<Response>;
}["bivarianceHack"]
```

A middleware function that may short-circuit or delegate to `next` to produce a response.

### `RouteBuilder`

```ts
RouteBuilder: any
```

Chainable, per-HTTP-method route registration methods, one per method plus `ws` for WebSockets.

- `route`: route<const Path extends string>(method: string | readonly string[], path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `get`: get<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `post`: post<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `put`: put<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `patch`: patch<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `delete`: delete<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `options`: options<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `head`: head<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `trace`: trace<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `connect`: connect<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

- `ws`: ws<const Path extends string>(path: Path, handler: WebSocketHandler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): ApiRoute<PathParams<Path>>;

### `Router`

```ts
Router: any
```

A mutable collection of routes and middleware, built with chainable per-method registration
methods (each returning the router itself for chaining) and a `use` method for middleware.

- `routes`: readonly routes: readonly ApiRoute[];

- `middleware`: readonly middleware: readonly Middleware[];

- `use`: use(...middleware: Middleware[]): Router;

- `route`: route<const Path extends string>(method: string | readonly string[], path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `get`: get<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `post`: post<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `put`: put<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `patch`: patch<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `delete`: delete<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `options`: options<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `head`: head<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `trace`: trace<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `connect`: connect<const Path extends string>(path: Path, handler: Handler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

- `ws`: ws<const Path extends string>(path: Path, handler: WebSocketHandler<PathParams<Path>>, options?: ApiRouteOptions<PathParams<Path>>): Router;

### `WebSocketHandler`

```ts
WebSocketHandler: {
  bivarianceHack(socket: WebSocketLike, context: ServerContext<RouteParams>): void | Promise<void>;
}["bivarianceHack"]
```

Handler invoked with a live {@link WebSocketLike} once a connection has been upgraded.

## Documentation navigation

[Previous](https://askrjs.com/docs/reference/api/server/root/index.md) | [Next](https://askrjs.com/docs/reference/api/server/http/index.md)
