Skip to content

Commit

Permalink
docs: Update middleware.gif (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachunei authored and dead-horse committed Sep 26, 2017
1 parent e1e030c commit 0168fd8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions docs/guide.md
Expand Up @@ -28,35 +28,16 @@ app.use(responseTime);

![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
3. Create another date to track response time
3. Create another date to track duration
4. Await control to the next middleware
5. Await immediately since `contentLength` only works with responses
6. Await upstream to Koa's noop middleware
7. Ignore setting the body unless the path is "/"
8. Set the response to "Hello World"
9. Ignore setting `Content-Length` when no body is present
10. Set the field
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';
});
```
5. Set the response body to "Hello World"
6. Calculate duration time
7. Output log line
8. Calculate response time
9. Set `X-Response-Time` header field
10. Hand off to Koa to handle the response

Next we'll look at the best practices for creating Koa middleware.

Expand Down
Binary file modified docs/middleware.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0168fd8

Please sign in to comment.