copy tweaks ()

This commit is contained in:
Zack Tanner 2016-05-12 23:27:18 -07:00 committed by Yiyu He
parent 41afac3eba
commit 808768a597
2 changed files with 5 additions and 5 deletions

View file

@ -41,19 +41,19 @@ app.listen(3000);
## Middleware ## Middleware
Koa is an middleware framework, it can take 3 different kind function as middleware: Koa is a middleware framework that can take 3 different kinds of functions as middleware:
* common function * common function
* async function * async function
* generatorFunction * generatorFunction
Here we write an logger middleware with different function. Here is an example of logger middleware with each of the different functions:
### Common function ### Common function
```js ```js
// Middleware normally take two parameters (ctx, next), ctx is the context for one request, // Middleware normally takes two parameters (ctx, next), ctx is the context for one request,
// next is an function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion. // 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) => { app.use((ctx, next) => {
const start = new Date(); const start = new Date();

View file

@ -1,7 +1,7 @@
# Guide # Guide
This guide covers Koa topics that are not directly API related, such as best practices for writing middleware, application structure suggestions, here we use async function as middleware you can also use commonFunction or generatorFunction which will be a little different. This guide covers Koa topics that are not directly API related, such as best practices for writing middleware and application structure suggestions. In these examples we use async functions as middleware - you can also use commonFunction or generatorFunction which will be a little different.
## Writing Middleware ## Writing Middleware