master
jongleberry 2017-02-25 00:30:11 -06:00
parent f191c0def4
commit d4b32234ea
2 changed files with 19 additions and 18 deletions

View File

@ -13,10 +13,11 @@
Koa is not bundled with any middleware.
## Installation
Koa requires __node v4.0.0__ or higher for (partial) ES2015 support.
Koa requires __node v7.6.0__ or higher for ES2015 and async function support.
```
$ npm install koa@next
$ npm install koa
```
## Hello koa
@ -49,6 +50,17 @@ Koa is a middleware framework that can take 3 different kinds of functions as mi
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 = new Date();
await next();
const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});
```
### Common function
```js
@ -64,17 +76,6 @@ app.use((ctx, next) => {
});
```
### ___async___ functions (Babel required)
```js
app.use(async (ctx, next) => {
const start = new Date();
await next();
const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});
```
### GeneratorFunction
To use generator functions, you must use a wrapper such as [co](https://github.com/tj/co) that is no longer supplied with Koa.
@ -120,7 +121,7 @@ app.use(convert(function *(next) {
## Babel setup
For Node 4.0 and Babel 6.0 you can setup like this:
For Node 4.0+ and Babel 6.0 you can setup like this:
```bash
$ npm install babel-register babel-plugin-transform-async-to-generator --save

View File

@ -1,18 +1,18 @@
# Installation
Koa requires __node v4.0.0__ or higher for (partial) ES2015 support.
Koa requires __node v7.6.0__ or higher for ES2015 and async function support.
You can quickly install a supposed version of node with your favorite version manager:
```bash
$ nvm install v4.0.0
$ nvm install 7
$ npm i koa
$ node my-koa-app.js
```
## Async Functions with Babel
To use `async` functions in Koa, we recommend using [babel's require hook](http://babeljs.io/docs/usage/require/).
To use `async` functions in Koa in versions of node < 7.6, we recommend using [babel's require hook](http://babeljs.io/docs/usage/require/).
```js
require('babel-core/register');
@ -183,7 +183,7 @@ ctx.cookies.set('name', 'tobi', { signed: true });
## app.context
`app.context` is the prototype from which `ctx` is created from.
`app.context` is the prototype from which `ctx` is created from.
You may add additional properties to `ctx` by editing `app.context`.
This is useful for adding properties or methods to `ctx` to be used across your entire app,
which may be more performant (no middleware) and/or easier (fewer `require()`s)