# Koa（Web 框架）

**Koa** 是一个富有表现力的 Node.js HTTP 中间件框架，使用 ES2017 异步函数，由 Express 背后的团队设计，旨在为 Web 应用和 API 提供一个更小、更具表现力的基础。<sup>[1](https://github.com/koajs/koa)</sup><sup> • </sup><sup>[2](https://subscription.packtpub.com/book/web_development/9781789345391/1/ch01lvl1sec03/what-is-koa)</sup> 开发始于 2013 年末，最初围绕当时新出现的 [JavaScript](https://www.edgepedia.org/zh/javascript) 生成器构建，此后该项目已转向以异步函数作为其中间件模型的基础。<sup>[2](https://subscription.packtpub.com/book/web_development/9781789345391/1/ch01lvl1sec03/what-is-koa)</sup><sup> • </sup><sup>[3](https://github.com/koajs/koa/blob/master/docs/guide.md)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 来自 Express 背后的团队、面向 Node.js 的富有表现力的 HTTP 中间件框架<sup>[1](https://github.com/koajs/koa)</sup> |
| 首次发布 | 2013 年 12 月 19 日的 0.1.1 版；0.2.1 版随后于 2013 年 12 月 30 日发布<sup>[4](https://github.com/koajs/koa/blob/master/History.md)</sup> |
| 当前主版本 | Koa 3，要求 Node.js v18.0.0 或更高版本<sup>[5](https://github.com/koajs/koa/blob/master/docs/migration-v2-to-v3.md)</sup> |
| 中间件模型 | 两类中间件函数；异步函数和 Promise 构成基础，上下文对象（`ctx`）在中间件之间传递<sup>[1](https://github.com/koajs/koa)</sup><sup> • </sup><sup>[3](https://github.com/koajs/koa/blob/master/docs/guide.md)</sup> |
| 吞吐量（基准测试） | 在一项对比中，Koa 3.2.1 以 3.463 毫秒的平均延迟处理了每秒 73,903 个请求，测试中最快框架的吞吐量约为它的 1.23 倍<sup>[6](https://www.techplained.com/nodejs-performance-tuning)</sup> |
| 生态规模 | 截至 2026 年 9 月 22 日，GitHub 上有 35,686 颗星，MIT 许可，2013 年首次发布于 npm；生态规模小于 Express 或 Fastify<sup>[7](https://webcodingcenter.com/express/express-koa-fastify-nestjs-and-hono--benchmarks-and-ecosystem-maturity.html)</sup><sup> • </sup><sup>[8](https://www.faun.dev/toolbox/fastify-vs-koa/)</sup> |

## 起源

Koa 由 Express 团队于 2013 年末启动，最初编写是为了利用当时新引入的 JavaScript 生成器来实现异步控制流。<sup>[2](https://subscription.packtpub.com/book/web_development/9781789345391/1/ch01lvl1sec03/what-is-koa)</sup> 更新日志显示，该项目在最初几周内进展迅速：0.1.1 版于 2013 年 12 月 19 日出现，到 0.2.0 版时，框架已增加了 `.throw(status, msg)` 支持、通过 “accepts” 模块进行的内容协商，以及中间件必须是生成器函数（GeneratorFunction）的断言。<sup>[4](https://github.com/koajs/koa/blob/master/History.md)</sup> 在最初发布公告时，Koa 要求在 `--harmony` 标志下使用 Node.js 11.9 或更高版本，这就是 Express 团队将其作为独立项目而非新版 Express 发布的原因。<sup>[9](https://news.ycombinator.com/item?id=6933358)</sup>

## 中间件与上下文对象

Koa 将自己描述为一个中间件框架，接受两种不同的函数作为中间件。<sup>[1](https://github.com/koajs/koa)</sup> 异步函数和 Promise 构成该框架的基础，使非阻塞代码能够以顺序风格编写。<sup>[3](https://github.com/koajs/koa/blob/master/docs/guide.md)</sup>

这种中间件模型通常被称为洋葱模型：每个中间件都可以在调用 `await next()` 之前和之后执行操作，因此 `next()` 调用之后的代码会在退出堆栈时运行。这种结构是开发者选择 Koa 而非 Express 风格中间件的主要原因——在后者中，下游代码无法轻易在链的其余部分完成后运行。<sup>[10](https://www.pkgpulse.com/guides/koa-vs-fastify-2026)</sup>

## 版本与不兼容变更

**Koa 3** 要求 Node.js v18.0.0 或更高版本，并移除了对旧版 v1.x 中间件签名的支持。此前通过 `koa-convert` 保持运行的生成器中间件必须重写为异步函数或返回 Promise 的函数。<sup>[5](https://github.com/koajs/koa/blob/master/docs/migration-v2-to-v3.md)</sup> Koa 3 还将 `http-errors` 依赖更新至 v2.0.0，这改变了 `ctx.throw()` 的签名。<sup>[5](https://github.com/koajs/koa/blob/master/docs/migration-v2-to-v3.md)</sup> 3.0.0 alpha 系列在 2025 年 2 月的更新日志中已经可见（3.0.0-alpha.3 日期为 2025 年 2 月 11 日），该项目现在通过 GitHub 的版本发布功能发布版本。<sup>[4](https://github.com/koajs/koa/blob/master/History.md)</sup>

## 性能及与 Express、Fastify 的比较

在一项已发布的基准测试中，Koa 3.2.1 达到每秒 73,903 个请求，平均延迟 3.463 毫秒，该对比中最快框架的吞吐量约为它的 1.23 倍；每个被测框架的性能大约只有原生 `node:http` 的一半，而最好与最差框架之间的差距，小于任何框架与完全不用框架之间的差距。<sup>[6](https://www.techplained.com/nodejs-performance-tuning)</sup><sup> • </sup><sup>[7](https://webcodingcenter.com/express/express-koa-fastify-nestjs-and-hono--benchmarks-and-ecosystem-maturity.html)</sup>

Koa 常被描述为一个极简、可组合的基础：Express 是灵活的默认选择，[Fastify](https://www.edgepedia.org/zh/fastify) 是性能优先的框架，带有基于模式的验证和插件，而 Koa 则是极简的中间件引擎。<sup>[11](https://qungs.com/articles/nodejs-frameworks-express-fastify-nest-koa-compared)</sup> 代价是生态规模。Koa 专用的组件种类不多，以 `(req, res, next)` 函数形式编写的 Connect 风格中间件需要适配器才能在 Koa 上运行，而 Fastify 可以通过兼容插件运行 Express 中间件。<sup>[8](https://www.faun.dev/toolbox/fastify-vs-koa/)</sup> Koa 自己的文档仍将其描述为面向 Web 应用和 API 的更小、更具表现力的基础。<sup>[10](https://www.pkgpulse.com/guides/koa-vs-fastify-2026)</sup>

## 基于 Koa 构建的项目

Koa 精简的核心使其成为更大框架的基础。阿里巴巴于 2018 年 3 月开源的 Egg.js 构建于 Koa 之上，增加了路由、日志和定时任务，同时完全支持异步函数，据报告其性能比早期版本提升了 30% 以上。<sup>[12](https://www.sohu.com/a/228497626_355140)</sup>

## References

1. [koajs/koa: Expressive middleware for node.js using ES2017 async functions, GitHub](https://github.com/koajs/koa)
2. [Introducing Koa, Server Side development with Node.js and Koa.js Quick Start Guide, Packt](https://subscription.packtpub.com/book/web_development/9781789345391/1/ch01lvl1sec03/what-is-koa)
3. [koa/docs/guide.md, GitHub](https://github.com/koajs/koa/blob/master/docs/guide.md)
4. [koa/History.md, GitHub](https://github.com/koajs/koa/blob/master/History.md)
5. [koa/docs/migration-v2-to-v3.md, GitHub](https://github.com/koajs/koa/blob/master/docs/migration-v2-to-v3.md)
6. [Node.js Performance Tuning: Handle 10x More Requests, TechPlained](https://www.techplained.com/nodejs-performance-tuning)
7. [Benchmarks – Express Alternatives, Web Coding Center](https://webcodingcenter.com/express/express-koa-fastify-nestjs-and-hono--benchmarks-and-ecosystem-maturity.html)
8. [Fastify vs Koa: Node.js Web Frameworks Comparison, Faun](https://www.faun.dev/toolbox/fastify-vs-koa/)
9. [Koa – Next-generation web framework for Node.js, Hacker News](https://news.ycombinator.com/item?id=6933358)
10. [Koa vs Fastify 2026, PkgPulse](https://www.pkgpulse.com/guides/koa-vs-fastify-2026)
11. [Node.js Frameworks: How Express, Fastify, Nest, and Koa Compare, Qungs](https://qungs.com/articles/nodejs-frameworks-express-fastify-nest-koa-compared)
12. [狼叔：Node全栈为前端带来更多可能, Sohu](https://www.sohu.com/a/228497626_355140)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Web frameworks and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/koa-web-framework

English original: https://www.edgepedia.org/koa-web-framework
