app.context

The prototype from which every per-request `ctx` object is created; extend it to add custom properties available on every context.

Since Koa 2 Spec ↗

Syntax

app.context

Returns

object — The shared context prototype object.

Examples

import Koa from 'koa';
import db from './db.js';

const app = new Koa();

// Attach db to every ctx
app.context.db = db;

app.use(async (ctx) => {
  const users = await ctx.db.query('SELECT * FROM users');
  ctx.body = users;
});

app.listen(3000);
Output
GET /  →  [{id:1,name:"Alice"}, ...]

Notes

Properties added to `app.context` are shared across all requests; avoid mutable per-request state here. For request-scoped values, use `ctx.state` instead. Extending `app.context` is idiomatic Koa and preferable to globals or module-level singletons.

See also