Expressive middleware for node.js using ES2017 async functions, now with less dependancies
 
 
Go to file
jongleberry e812339033 docs: create v2 Migration document (#931)
* 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++
2017-03-07 22:59:42 -08:00
benchmarks Remove unused http variable 2016-03-12 17:53:14 -07:00
docs docs: create v2 Migration document (#931) 2017-03-07 22:59:42 -08:00
lib docs: create v2 Migration document (#931) 2017-03-07 22:59:42 -08:00
test res: use http.ServerResponse._header when accessors exist (#930) 2017-03-07 22:59:24 -08:00
.editorconfig add editorconfig 2015-10-13 09:23:57 +02:00
.eslintrc.yml Lint JavaScript in Markdown 2016-03-16 16:50:10 +01:00
.gitignore 0.14.0 2014-12-15 10:13:40 -08:00
.travis.yml travis: test node@8 nightly 2017-02-25 14:09:51 -06:00
AUTHORS update AUTHORS 2015-10-12 00:04:06 -07:00
CODE_OF_CONDUCT.md add CODE_OF_CONDUCT.md 2016-03-12 14:22:16 -08:00
History.md history++ 2017-02-25 00:38:20 -06:00
LICENSE chore(license): update license year to 2016 2016-03-12 14:19:44 -08:00
Makefile test: remove babel tests as they are no longer needed in node v7.6 2017-02-25 00:05:25 -06:00
Readme.md docs: create v2 Migration document (#931) 2017-03-07 22:59:42 -08:00
package.json update engines in package.json for 7.6.0 min node (#911) 2017-02-25 21:29:16 +08:00

Readme.md

koa middleware framework for nodejs

gitter NPM version build status Test coverage OpenCollective Backers OpenCollective Sponsors

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

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