* Give v2 migration documentation its own document. Incorporate docs from #533 * Fix mis-capitalization of Koa * Remove unnecessary Dependency section * Hint at koa-convert enabled compatibility * Add section on constructing with new * Clarify es6 constructors are used * Fix varying capitalization * Restore mistakenly removed Dependency changes section * v1.x should not receive feature updates * Add next() to signature, add missing backticks * docs++
14 KiB
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 v7.6.0 or higher for ES2015 and async function support.
$ npm install koa
Hello koa
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.
- Workshop - A workshop to learn the basics of koa, Express' spiritual successor.
- Introduction Screencast - An introduction to installing and getting started with Koa
Middleware
Koa is a middleware framework that can take 3 different kinds of functions as middleware:
- common function
- async function
Here is an example of logger middleware with each of the different functions:
async functions (node v7.6+)
app.use(async (ctx, next) => {
const start = new Date();
await next();
const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});
Common function
// 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 = new Date();
return next().then(() => {
const ms = new Date() - 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 will be removed in v3
Please see the Migration Guide for more information on upgrading from v1.x and using v1.x middleware with v2.x.
Babel setup
If you're not using node v7.6+
, we recommend setting up babel
with babel-preset-env
:
$ npm install babel-register babel-preset-env --save
Setup babel-register
in your entry file:
require('babel-register');
And have your .babelrc
setup:
{
"presets": [
["env", {
"targets": {
"node": true
}
}]
]
}
Troubleshooting
Check the Troubleshooting Guide or Debugging Koa in the general Koa guide.
Running tests
$ make test
Authors
See AUTHORS.
Community
- API documentation
- Badgeboard and list of official modules
- Examples
- Middleware list
- Wiki
- G+ Community
- Reddit Community
- Mailing list
- Guide
- FAQ
- 中文文档
- #koajs on freenode
Backers
Support us with a monthly donation and help us continue our activities.
Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site.
License
MIT