# Server

> Build a context-first HTTP application with explicit routing, middleware, actions, realtime, and probes.

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

Status: stable. Packages: @askrjs/server.

**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.

## Example

Build the HTTP surface from a router and context-first handlers; bind input and return an explicit response helper from the same boundary.

```tsx
import { createRouter, created } from '@askrjs/server';

export const router = createRouter().post('/projects', async (context) => {
  const input = await context.bind<CreateProjectInput>();
  const project = await projects.create(input);
  return created(project);
});
```

## Application and router

createServerApp({ router }) is the canonical low-level HTTP composition path. Add other cross-cutting options to the same object as needed. It produces a ServerApp whose only contract is fetch(request): Promise&lt;Response&gt; — nothing Node-, Bun-, or Deno-specific leaks into the return type. The router itself owns path matching, per-route middleware, and WebSocket upgrade wiring; the app wraps it with cross-cutting options like error handling and probe checks. The direct Router overload and raw routes option remain compatibility boundaries for existing integrations, not peer application guidance.

## Context-first handlers

Every handler and every Middleware receives a single ServerContext argument instead of separate request/response objects — request, url, params, headers, query, state, auth, and an AbortSignal all live on ctx. Dependencies like a database client are deliberately not part of that context; the README shows creating them at your composition root and passing them into route registration functions, keeping ctx reserved for request-scoped data. Because the context also carries every response helper (ctx.ok, ctx.notFound, and so on), a handler can build its whole response without importing anything else.

## Response helpers

ctx.ok(), ctx.notFound(), ctx.created(), ctx.problem(), and the rest of the status-code helpers are methods on ServerContext, so a handler returns a Response by calling ctx.&lt;name&gt;(value) rather than constructing new Response(...) by hand. The same functions — json, text, redirect, setCookie, clearCookie, challenge, and friends — are also exported standalone from @askrjs/server for use outside a request context, such as inside an onError handler. setCookie() and clearCookie() take a Response and return a modified one; per the README they only touch response headers and have no opinion on session storage or credential validation.

## Production boundary

ServerAppOptions accepts an onError(error, context) hook that runs for an unhandled exception a handler or middleware throws, so you control what most errors turn into instead of leaking a stack trace to the client — but it's the fallback, not the only path: a few framework-recognized failures (an oversized request body, a malformed path parameter, a ctx.bind() failure) are converted to a fixed problem+json response before onError is ever consulted, so don't rely on it to customize those specific cases. The OpenAPI layer defaults to validateResponses: false and only turns response validation on when you explicitly opt in outside production, because checking every response against its schema costs real time on every request. The package is still published pre-1.0 (0.2.x), so pin versions and review release notes before moving a production app between versions.

## In this section

- [HTTP Routing](https://askrjs.com/docs/server/http-routing/index.md): HTTP Routing at the Askr server boundary, with validated input and explicit failure states.
- [Context and Responses](https://askrjs.com/docs/server/context-and-responses/index.md): Context and Responses at the Askr server boundary, with validated input and explicit failure states.
- [Request Binding](https://askrjs.com/docs/server/request-binding/index.md): Request Binding at the Askr server boundary, with validated input and explicit failure states.
- [Middleware and Security](https://askrjs.com/docs/server/middleware-and-security/index.md): Middleware and Security at the Askr server boundary, with validated input and explicit failure states.
- [Server Actions](https://askrjs.com/docs/server/server-actions/index.md): Server Actions at the Askr server boundary, with validated input and explicit failure states.
- [Realtime](https://askrjs.com/docs/server/realtime/index.md): Realtime at the Askr server boundary, with validated input and explicit failure states.
- [Probes](https://askrjs.com/docs/server/probes/index.md): Probes at the Askr server boundary, with validated input and explicit failure states.
- [Authentication](https://askrjs.com/docs/authentication/index.md): Authentication at the Askr server boundary, with validated input and explicit failure states.
- [Model and Sessions](https://askrjs.com/docs/authentication/model-and-sessions/index.md): Model and Sessions at the Askr server boundary, with validated input and explicit failure states.
- [Authorization Requirements](https://askrjs.com/docs/authentication/authorization/index.md): Authorization Requirements at the Askr server boundary, with validated input and explicit failure states.
- [JWT](https://askrjs.com/docs/authentication/jwt/index.md): JWT at the Askr server boundary, with validated input and explicit failure states.
- [OIDC](https://askrjs.com/docs/authentication/oidc/index.md): OIDC at the Askr server boundary, with validated input and explicit failure states.
- [Framework Auth Routes](https://askrjs.com/docs/authentication/framework-routes/index.md): Framework Auth Routes at the Askr server boundary, with validated input and explicit failure states.
- [HTTP Contracts](https://askrjs.com/docs/http-contracts/index.md): HTTP Contracts at the Askr server boundary, with validated input and explicit failure states.
- [Typed Clients](https://askrjs.com/docs/http-contracts/typed-clients/index.md): Typed Clients at the Askr server boundary, with validated input and explicit failure states.
- [Results, Errors, and Cancellation](https://askrjs.com/docs/http-contracts/results-and-errors/index.md): Results, Errors, and Cancellation at the Askr server boundary, with validated input and explicit failure states.
- [Codecs and Serialization](https://askrjs.com/docs/http-contracts/codecs/index.md): Codecs and Serialization at the Askr server boundary, with validated input and explicit failure states.
- [Client Middleware](https://askrjs.com/docs/http-contracts/client-middleware/index.md): Client Middleware at the Askr server boundary, with validated input and explicit failure states.
- [Schemas](https://askrjs.com/docs/http-contracts/schemas/index.md): Schemas at the Askr server boundary, with validated input and explicit failure states.
- [OpenAPI](https://askrjs.com/docs/http-contracts/open-api/index.md): OpenAPI at the Askr server boundary, with validated input and explicit failure states.
- [MCP](https://askrjs.com/docs/mcp/index.md): MCP at the Askr server boundary, with validated input and explicit failure states.
- [Primitives](https://askrjs.com/docs/mcp/primitives/index.md): Primitives at the Askr server boundary, with validated input and explicit failure states.
- [Schemas, Auth, Context, and Progress](https://askrjs.com/docs/mcp/context-and-progress/index.md): Schemas, Auth, Context, and Progress at the Askr server boundary, with validated input and explicit failure states.
- [HTTP Transport](https://askrjs.com/docs/mcp/http-transport/index.md): HTTP Transport at the Askr server boundary, with validated input and explicit failure states.
- [SSE Transport](https://askrjs.com/docs/mcp/sse-transport/index.md): SSE Transport at the Askr server boundary, with validated input and explicit failure states.
- [Sessions](https://askrjs.com/docs/mcp/sessions/index.md): Sessions at the Askr server boundary, with validated input and explicit failure states.
- [Stdio Transport](https://askrjs.com/docs/mcp/stdio-transport/index.md): Stdio Transport at the Askr server boundary, with validated input and explicit failure states.
- [Platform Services](https://askrjs.com/docs/platform-services/index.md): Platform Services at the Askr server boundary, with validated input and explicit failure states.
- [Node Runtime](https://askrjs.com/docs/platform-services/node-runtime/index.md): Node Runtime at the Askr server boundary, with validated input and explicit failure states.
- [Internationalization](https://askrjs.com/docs/platform-services/internationalization/index.md): Internationalization at the Askr server boundary, with validated input and explicit failure states.
- [OpenTelemetry](https://askrjs.com/docs/platform-services/open-telemetry/index.md): OpenTelemetry at the Askr server boundary, with validated input and explicit failure states.

## Documentation navigation

[Previous](https://askrjs.com/docs/rendering/static-site-generation/index.md) | [Next](https://askrjs.com/docs/server/http-routing/index.md)
