Language : [🇺🇸 English] | 🇨🇳 中文
This project is for learning the basics and advanced usage of the Koa framework, suitable for both beginners and advanced users.
- Bilingual Comments: All example files include detailed comments in both English and Chinese, making it easy for developers from different backgrounds to understand.
- Step-by-Step Examples: Each key concept (middleware, routing, error handling, async, etc.) is demonstrated in a separate file, allowing you to learn and test incrementally.
- Covers Latest Koa3 Features: Examples are based on Koa3, covering official best practices and common middleware, compatible with Node.js 16+, and including TypeScript support notes.
- Practical RESTful API Demo: Built-in user route examples show how to build RESTful APIs with Koa.
- Developer Friendly: Supports
nodemonfor automatic reloads, and provides simple development scripts.
- Clone the repository
git clone https://github.com/HowieCong/Learn-KOA.git cd Learn-KOA - Install dependencies
npm install
- Run any example (e.g., Quick Start)
node src/01_快速入门.js # Visit http://localhost:4001 in your browser - Recommended development mode (auto-reload)
npm run dev # By default runs src/08_错误处理.js; you can modify the script in package.json or specify another file directly
package.json
README.md
src/
01_快速入门.js # Quick start with Koa
02_中间件.js # Middleware usage
03_洋葱圈模型.js # Onion model (middleware execution order)
04_koa处理同步数据.js # Handling synchronous data
05_koa处理异步数据.js # Handling asynchronous data
06_路由.js # Basic routing
07_koa-router.js # Advanced routing with koa-router and koa-body
08_错误处理.js # Error handling in Koa
async.js # async/await demo
await.js # await demo
router/
user.route.js # User routes for RESTful API demo
- koa ^3.0.1
- koa-router ^13.1.1
- koa-body ^6.0.1
- koa-json-error ^3.1.2
- cross-env ^7.0.3
- nodemon (dev dependency)
npm run dev— Start with nodemon for development (auto-reload, runssrc/08_错误处理.js)npm run prod— Start in production mode (usescross-envto setNODE_ENV=production)
You can use nodemon for automatic restarts:
npx nodemon src/01_快速入门.js
# or any other example filenode src/01_快速入门.jsVisit: http://localhost:4001
node src/02_中间件.jsVisit: http://localhost:4002
node src/03_洋葱圈模型.jsCheck the console output for middleware execution order.
node src/04_koa处理同步数据.jsVisit: http://localhost:4004
node src/05_koa处理异步数据.jsVisit: http://localhost:4005
node src/06_路由.jsVisit: http://localhost:4006
node src/07_koa-router.jsVisit: http://localhost:4007/users
- Supports GET
/users?start=18&end=20for user list filtering - Supports GET
/users/:idfor user detail - Supports POST
/usersfor user creation (see code for details)
node src/08_错误处理.jsDemonstrates how to handle errors globally in Koa.
node src/async.js
node src/await.jsShowcases how to use async/await in Koa middleware.
The src/router/user.route.js file contains user-related RESTful API routes, which are imported and used in 07_koa-router.js.
| Feature | Koa2 | Koa3 (latest) |
|---|---|---|
| Node.js Version | Requires Node.js >= 7.6 (async/await) | Requires Node.js >= 16 |
| Middleware Signature | async (ctx, next) |
async (ctx, next) (unchanged) |
| Middleware Compose | Uses koa-compose internally |
Uses @koajs/compose |
| Error Handling | Manual try/catch in middleware |
Improved error propagation |
| Context Extension | Directly on ctx |
Same, but with stricter typings |
| TypeScript Support | Community maintained | Official TypeScript typings |
| Package Name | koa |
koa (v3.x.x) |
| Breaking Changes | Some plugins may not be compatible | Some APIs and plugins updated |
Summary:
Koa3 requires a newer Node.js version, has better TypeScript support, and some internal improvements. Most middleware usage remains the same, but always check plugin compatibility.
- Read the comments in each example file (bilingual: English & Chinese).
- Try modifying the middleware and routes to deepen your understanding.
- Explore the official Koa documentation for more advanced features.
- For TypeScript users, refer to Koa's official type definitions.
Contributions are welcome!
You can:
- Add new Koa example files (such as advanced middleware, routing, error handling, etc.)
- Improve existing code or comments (including English/Chinese translation, code style, etc.)
- Fix errors in documentation or code
Contribution process:
- Fork this repository
- Create a new branch for your changes
- Submit a PR and briefly describe your changes
I will review and merge valuable contributions promptly. Thank you for your support!
For more information, please refer to the Koa official documentation.