koajs /koa

Expressive middleware for node.js using ES2017 async functions

★ 35,682⑂ 3,247👁 789⑃ 993● 935▤ 19◈ 38
JavaScriptMITCreated 2013-07-20koajs.com
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 IncomingMessa

Top issues

Discussions

Latest release

v3.3.0 v3.3.0 2026-11-03
What'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

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON