docs
This commit is contained in:
parent
4a0c55cc96
commit
33221d6574
1 changed files with 27 additions and 1 deletions
|
@ -53,6 +53,32 @@ app.use(function(next){
|
||||||
12. Set `X-Response-Time` header field before response
|
12. Set `X-Response-Time` header field before response
|
||||||
13. Hand off to Koa to handle the response
|
13. Hand off to Koa to handle the response
|
||||||
|
|
||||||
|
Note that the final middleware (step __6__) yields to what looks to be nothing - it's actually
|
||||||
|
yielding to a no-op generator within Koa. This is so that every middleware can conform with the
|
||||||
|
same API, and may be placed before or after others. If you removed `yield next;` from the furthest
|
||||||
|
"downstream" middleware everything would function appropritaely, however it would no longer conform
|
||||||
|
to this behaviour.
|
||||||
|
|
||||||
|
For example this would be fine:
|
||||||
|
|
||||||
|
```js
|
||||||
|
app.use(function(){
|
||||||
|
return function *response(){
|
||||||
|
if ('/' != this.url) return;
|
||||||
|
this.body = 'Hello World';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
It's important to note that not all middleware will need to conform, the best example of this
|
||||||
|
is a router, which are typically always acting as end-points, or downstream middleware:
|
||||||
|
|
||||||
|
````js
|
||||||
|
app.use(get('/', function *(){
|
||||||
|
this.body = 'Hello World';
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
Next we'll look at the best practices for creating Koa middleware.
|
Next we'll look at the best practices for creating Koa middleware.
|
||||||
|
|
||||||
## Middleware Best Practices
|
## Middleware Best Practices
|
||||||
|
|
Loading…
Reference in a new issue