docs: add note about confusing context accessors

master
Jonathan Ong 2014-02-13 18:23:45 -08:00
parent a9149f07ad
commit abb5686d5b
1 changed files with 7 additions and 5 deletions

View File

@ -3,12 +3,8 @@
A Koa Context encapsulates node's `request` and `response` objects A Koa Context encapsulates node's `request` and `response` objects
into a single object which provides many helpful methods for writing into a single object which provides many helpful methods for writing
web applications and APIs. web applications and APIs.
Many accessors and methods simply delegate to their `ctx.request` or `ctx.response`
equivalents for convenience, and are otherwise identical.
These operations are used so frequently in HTTP server development These operations are used so frequently in HTTP server development
that they are added at this level, instead of a higher level framework, that they are added at this level instead of a higher level framework,
which would force middleware to re-implement this common functionality. which would force middleware to re-implement this common functionality.
A `Context` is created _per_ request, and is referenced in middleware A `Context` is created _per_ request, and is referenced in middleware
@ -23,6 +19,12 @@ app.use(function *(){
}); });
``` ```
Many of the context's accessors and methods simply delegate to their `ctx.request` or `ctx.response`
equivalents for convenience, and are otherwise identical.
In general, getters are delegated to `ctx.request` and setters are delegated to `ctx.response`.
For example, `this.length` returns the request's `content-length`, whereas `this.length = 1024` sets the response's `content-length` to `1024`.
If this confuses you, simply use `ctx.request` or `ctx.response` directly.
## API ## API
`Context` specific methods and accessors. `Context` specific methods and accessors.