Koa
Minimal Node.js web framework built on async middleware, an explicit context object, and upstream and downstream control flow.
Official Documentation ↗3.3.0. Check the official docs for the latest API surface.Installation
npm install koa @koa/routerpnpm add koa @koa/routeryarn add koa @koa/routerMethods
No methods match this filter.
Koa is a small Node.js framework built around async functions. Instead of augmenting req and res, it exposes one context object per request and lets middleware decide when to hand control downstream, which makes response timing, error handling, and composition explicit.
Overview
Every request flows through a stack of async middleware. Calling await next() runs the downstream middleware and returns control, so cleanup and header changes can happen after the response is produced. Koa itself has no router, body parser, or static file support; those are separate packages, which keeps the core small and the dependency surface visible.
Installation
Install koa and, for most APIs, @koa/router. Body parsing needs @koa/bodyparser, and static assets need @koa/static or a CDN. Koa 3 requires a current Node.js LTS release. Register middleware in order: error handling and logging first, body parsing next, routes last, with router.allowedMethods() closing the chain.
Core API walkthrough
new Koa() creates the application and app.listen binds a port. app.use registers async middleware, where next() returns a promise. ctx.body and ctx.status build the response, with objects serialized as JSON. ctx.throw raises an HTTP error that the framework renders, and @koa/router provides path parameters plus method routing through router.routes().
Pitfalls
Forgetting to await next() silently short-circuits the stack. Errors thrown in async middleware must be caught by an upstream try/catch or emitted on the app error event, otherwise the process can exit. Koa does not parse bodies, and it does not set CORS headers by default. Also avoid writing to ctx.body twice; the last assignment wins.
Verification
Send requests with curl or Supertest and check status, content type, and body. Add a route that throws to confirm the error handler returns JSON instead of an HTML stack trace, and assert that the response-time header is present to prove the upstream/downstream ordering.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://koajs.com/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:37.942Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- By leveraging async functions, Koa allows you to ditch callbacks and greatly increase error-handling. (https://koajs.com/)
- The default error handler also won’t output errors when err.status is 404 or err.expose is true . (https://koajs.com/)
- In either case an app-level “error” is emitted for logging purposes. (https://koajs.com/)
Security notes
Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://koajs.com/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:37.942Z.
- Most of the reverse proxy(nginx) set x-forwarded-for via proxy_add_x_forwarded_for , which poses a certain security risk. (https://koajs.com/)
- A malicious attacker can forge a client’s ip address by forging a X-Forwarded-For request header. (https://koajs.com/)
FAQ
- How do I parse JSON request bodies?
- Koa core does not parse bodies. Register @koa/bodyparser before your routes and read ctx.request.body; without it the body stays undefined and validation silently passes on empty input.
- Why is ctx.body empty after my middleware?
- A middleware that does not await next() stops the chain, and one that sets ctx.body later overwrites earlier values. Ensure each middleware awaits downstream work before writing the response.
- Does Koa include routing and static files?
- No. Koa is intentionally minimal. Add @koa/router for routing, @koa/bodyparser for bodies, and @koa/static or a CDN for assets, or the application stays a single middleware stack.
Report a problem with this API docOpens a pre-filled issue on GitHub.