# Build backends with Oak

Oak provides a Koa/Express-style middleware and context model. It fits Deno backend teams that already think in middleware or need a fuller routing layer.

```bash
deno add jsr:@oak/oak
```

```ts
import { Application } from "jsr:@oak/oak/application";
import { Router } from "jsr:@oak/oak/router";

const app = new Application();
const router = new Router();

router.get("/health", (ctx) => {
  ctx.response.body = { ok: true };
});

app.use(async (ctx, next) => {
  const started = performance.now();
  await next();
  ctx.response.headers.set("server-timing", `app;dur=${performance.now() - started}`);
});
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });
```

```bash
deno run --allow-net=0.0.0.0:8000 main.ts
```

Middleware order is control flow. Put error handling, request IDs, and security headers before routes; make 404 mapping and audit logging observe downstream results. Do not create a new database pool inside every request.

## Oak versus Hono

- Koa/Express experience and context mutation: Oak is familiar.
- One small API across Deno, Workers, Bun, and Node: evaluate Hono first.
- Only a few endpoints: begin with `Deno.serve`.

Official references: [oak on JSR](https://jsr.io/@oak/oak) and [Router API](https://jsr.io/@oak/oak/doc/router).
