docs: Update middleware.gif (#1052)

This commit is contained in:
Pedro Pablo Aste Kompen 2017-09-26 01:22:22 -03:00 committed by Yiyu He
parent e1e030cc3d
commit 0168fd87a8
2 changed files with 8 additions and 27 deletions

View file

@ -28,35 +28,16 @@ app.use(responseTime);
![koa middleware](/docs/middleware.gif) ![koa middleware](/docs/middleware.gif)
1. Create a date to track duration 1. Create a date to track response time
2. Await control to the next middleware 2. Await control to the next middleware
3. Create another date to track response time 3. Create another date to track duration
4. Await control to the next middleware 4. Await control to the next middleware
5. Await immediately since `contentLength` only works with responses 5. Set the response body to "Hello World"
6. Await upstream to Koa's noop middleware 6. Calculate duration time
7. Ignore setting the body unless the path is "/" 7. Output log line
8. Set the response to "Hello World" 8. Calculate response time
9. Ignore setting `Content-Length` when no body is present 9. Set `X-Response-Time` header field
10. Set the field 10. Hand off to Koa to handle the response
11. Output log line
12. Set `X-Response-Time` header field before response
13. Hand off to Koa to handle the response
Note that the final middleware (step __6__) await to what looks to be nothing — it's actually
yielding to a no-op promise 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 `next();` from the furthest
"downstream" middleware everything would function appropriately, however it would no longer conform
to this behaviour.
For example this would be fine:
```js
app.use(async function response(ctx, next) {
if ('/' != this.url) return;
ctx.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.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 782 KiB

After

Width:  |  Height:  |  Size: 66 KiB