35,682
stars
+1+0+4
3,247
forks
789
watchers
993
prs
935
issues
19
releases
38
discussions
README
[![gitter][gitter-image]][gitter-url]
[![NPM version][npm-image]][npm-url]
[![build status][github-action-image]][github-action-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![OpenCollective Backers][backers-image]](backers)
[![OpenCollective Sponsors][sponsors-image]](sponsors)
[![PR's Welcome][pr-welcoming-image]][pr-welcoming-url]
Expressive HTTP middleware framework for node.js to make web applications and APIs more enjoyable to write. Koa's middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.
Only methods that are common to nearly all HTTP servers are integrated directly into Koa's small ~570 SLOC codebase. This
includes things like content negotiation, normalization of node inconsistencies, redirection, and a few others.
Koa is not bundled with any middleware.
Installation
Koa requires __node v18.0.0__ or higher for ES2015 and async function support.
sh
npm install koa
Hello Koa
js
const Koa = require('koa');
const app = new Koa();
// response
app.use(ctx = {
ctx.body = 'Hello Koa';
});
app.listen(3000);
Getting started
- Kick-Off-Koa - An intro to Koa via a set of self-guided workshops.
- Guide - Go straight to the docs.
Middleware
Koa is a middleware framework that can take two different kinds of functions as middleware:
async function
common function
Here is an example of logger middleware with each of the different functions:
___async___ functions (node v7.6+)
js
app.use(async (ctx, next) = {
const start = Date.now();
await next();
const ms = Date.now() - start;
console.log(${ctx.method} ${ctx.url} - ${ms}ms);
});
Common function
js
// Middleware normally takes two parameters (ctx, next), ctx is the context for one request,
// next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.
app.use((ctx, next) = {
const start = Date.now();
return next().then(() = {
const ms = Date.now() - start;
console.log(${ctx.method} ${ctx.url} - ${ms}ms);
});
});
Koa v1.x Middleware Signature
The middleware signature changed between v1.x and v2.x. The older signature is deprecated.
Old signature middleware support has been removed in v3
Please see the Migration Guide from v2.x to v3.x for information on upgrading from v2.x to v3.x, and the Migration Guide from v1.x to v2.x for information on upgrading from v1.x to v2.x.
Context, Request and Response
Each middleware receives a Koa Context object that encapsulates an incoming
http message and the corresponding response to that message. ctx is often used
as the parameter name for the context object.
js
app.use(async (ctx, next) = { await next(); });
Koa provides a Request object as the request property of the Context.
Koa's Request object provides helpful methods for working with
http requests which delegate to an IncomingMessaTop issues
#281integrate proxy-addr18 comments · 2014-05-07#1547bug report! If koa and http2server are used together, the response with method "head" request will never end!13 comments · 2021-06-17#827request.ip via x-forwarded-for should strip out ports12 comments · 2016-10-04#1607Distribute TypeScript types with koa package8 comments · 2021-11-05#1869Request for Maintainers4 comments · 2025-04-06#1903[feat] Update `accepts` module3 comments · 2025-08-22#2026[fix] request.subdomains treats a trailing-dot hostname as an extra subdomain2 comments · 2026-11-01#1915Package [email protected] has been deprecated2 comments · 2025-10-29
Discussions
#2038Relia: Koa request tracing that explains what broke in plain words0 comments · 2026-11-05#2035Koa error middleware that keeps app.on('error') working0 comments · 2026-11-03#1981Do after response work1 comments · 2026-06-21#1905Why is there both a v2.16.0 tag and a 2.16.0 tag?1 comments · 2025-08-25#1901Share a Koa Learning Repo0 comments · 2025-08-13#1974[fix] read large files in node, there will be a wrong package in koa, memory overflow5 comments · 2025-05-29#1872Planning for koa-static-cache v6 — Feedback, Ideas & Roadmap0 comments · 2025-04-09#1871ctx.body vs ctx.response.body1 comments · 2025-04-07
Latest release
v3.3.0 v3.3.0 2026-11-03What's Changed chore: Tell dependabot to update GH Actions by @olleolleolle in https://github.com/koajs/koa/pull/1963 build(deps): bump actions/checkout from 4 to 6 by @dependabot[bot] in https://github.com/koajs/koa/pull/1966 build(deps): bump actions/setup-node from 4 to 6 by @dependabot[bot] in https://github.com/koajs/koa/pull/1965 build(deps): bump codecov/codecov-action from 5 to 6 by @dependabot[bot] in https://github.com/koajs/koa/pull/1964 build(deps-dev): bump flatted from 3.3.3 to 3.4.2 by @dependabot[bot] in https://github.com/koajs/koa/pull/1945 build(deps-dev): bump c8 from