ctx.app

A reference to the Koa `Application` instance, giving middleware access to app-level settings and the event emitter.

Since Koa 2 Spec ↗

Syntax

ctx.app

Returns

Application — The Koa `Application` instance that created this context.

Examples

import Koa from 'koa';

const app = new Koa();
app.config = { siteName: 'jsschools' }; // custom property on app

app.use(async (ctx) => {
  ctx.body = { site: ctx.app.config.siteName, env: ctx.app.env };
});

app.listen(3000);
Output
{"site":"jsschools","env":"development"}
// Emit a custom event from middleware
app.use(async (ctx, next) => {
  await next();
  ctx.app.emit('request:complete', ctx);
});
Output
(custom event emitted after each request)

Notes

Most application-level settings are better accessed via `app.context` properties or `ctx.state`. Use `ctx.app` sparingly; primarily useful when you need to emit events or access `app.env` / `app.proxy` dynamically inside a middleware.

See also