2013-11-13 17:01:15 +00:00
|
|
|
# Context
|
|
|
|
|
|
|
|
A Koa Context encapsulates node's `request` and `response` objects
|
|
|
|
into a single object which provides many helpful methods for writing
|
|
|
|
web applications and APIs.
|
|
|
|
|
2013-12-20 10:10:31 +00:00
|
|
|
Many accessors and methods simply delegate to their `ctx.request` or `ctx.response`
|
2013-11-13 17:01:15 +00:00
|
|
|
equivalents for convenience, and are otherwise identical.
|
|
|
|
|
|
|
|
These operations are used so frequently in HTTP server development
|
|
|
|
that they are added at this level, instead of a higher level framework,
|
2013-12-22 12:26:30 +00:00
|
|
|
which would force middleware to re-implement this common functionality.
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-11-20 06:40:52 +00:00
|
|
|
A `Context` is created _per_ request, and is referenced in middleware
|
2013-12-19 04:35:38 +00:00
|
|
|
as the receiver, or the `this` identifier, as shown in the following
|
|
|
|
snippet:
|
|
|
|
|
|
|
|
```js
|
|
|
|
app.use(function *(){
|
|
|
|
this; // is the Context
|
|
|
|
this.request; // is a koa Request
|
|
|
|
this.response; // is a koa Response
|
|
|
|
});
|
|
|
|
```
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
|
|
`Context` specific methods and accessors.
|
|
|
|
|
|
|
|
### ctx.req
|
|
|
|
|
|
|
|
Node's `request` object.
|
|
|
|
|
|
|
|
### ctx.res
|
2013-11-20 06:40:52 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
Node's `response` object.
|
|
|
|
|
2013-12-26 08:11:14 +00:00
|
|
|
Note: bypassing Koa's response handling and calling `res.writeHead()`, `res.write()` or `res.end()` is __not__ supported.
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
### ctx.request
|
|
|
|
|
|
|
|
A koa `Request` object.
|
|
|
|
|
|
|
|
### ctx.response
|
2013-11-20 06:40:52 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
A koa `Response` object.
|
|
|
|
|
|
|
|
### ctx.app
|
|
|
|
|
|
|
|
Application instance reference.
|
|
|
|
|
|
|
|
### ctx.cookies.get(name, [options])
|
|
|
|
|
|
|
|
Get cookie `name` with `options`:
|
|
|
|
|
|
|
|
- `signed` the cookie requested should be signed
|
|
|
|
|
2013-12-18 09:31:33 +00:00
|
|
|
Note: koa uses the [cookies](https://github.com/jed/cookies) module where options are simply passed.
|
2013-11-20 06:40:52 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
### ctx.cookies.set(name, value, [options])
|
|
|
|
|
|
|
|
Set cookie `name` to `value` with `options`:
|
|
|
|
|
|
|
|
- `signed` sign the cookie value
|
|
|
|
- `expires` a `Date` for cookie expiration
|
|
|
|
- `path` cookie path, `/'` by default
|
|
|
|
- `domain` cookie domain
|
|
|
|
- `secure` secure cookie
|
|
|
|
- `httpOnly` server-accessible cookie, __true__ by default
|
|
|
|
|
2013-12-18 09:31:33 +00:00
|
|
|
Note: koa uses the [cookies](https://github.com/jed/cookies) module where options are simply passed.
|
2013-11-20 06:40:52 +00:00
|
|
|
|
|
|
|
### ctx.throw(msg, [status])
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
Helper method to throw an error with a `.status` property
|
2013-12-19 04:35:38 +00:00
|
|
|
defaulting to `500` that will allow Koa to respond appropriately.
|
2013-12-18 09:30:27 +00:00
|
|
|
The following combinations are allowed:
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
```js
|
2013-11-20 06:40:52 +00:00
|
|
|
this.throw(403)
|
|
|
|
this.throw('name required', 400)
|
2013-12-20 23:34:48 +00:00
|
|
|
this.throw(400, 'name required')
|
2013-11-20 06:40:52 +00:00
|
|
|
this.throw('something exploded')
|
2013-11-13 17:01:15 +00:00
|
|
|
```
|
|
|
|
|
2013-12-20 00:23:29 +00:00
|
|
|
For example `this.throw('name required', 400)` is equivalent to:
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
var err = new Error('name required');
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that these are user-level errors and are flagged with
|
2013-11-20 06:40:52 +00:00
|
|
|
`err.expose` meaning the messages are appropriate for
|
2013-11-13 17:01:15 +00:00
|
|
|
client responses, which is typically not the case for
|
|
|
|
error messages since you do not want to leak failure
|
2013-12-19 04:35:38 +00:00
|
|
|
details.
|
2013-12-25 06:59:55 +00:00
|
|
|
|
|
|
|
## Request aliases
|
|
|
|
|
|
|
|
The following accessors and alias [Request](request.md) equivalents:
|
|
|
|
|
|
|
|
- `ctx.header`
|
|
|
|
- `ctx.method`
|
|
|
|
- `ctx.method=`
|
|
|
|
- `ctx.url`
|
|
|
|
- `ctx.url=`
|
|
|
|
- `ctx.path`
|
|
|
|
- `ctx.path=`
|
|
|
|
- `ctx.query`
|
|
|
|
- `ctx.query=`
|
|
|
|
- `ctx.querystring`
|
|
|
|
- `ctx.querystring=`
|
|
|
|
- `ctx.length`
|
|
|
|
- `ctx.host`
|
|
|
|
- `ctx.fresh`
|
|
|
|
- `ctx.stale`
|
|
|
|
- `ctx.socket`
|
|
|
|
- `ctx.protocol`
|
|
|
|
- `ctx.secure`
|
|
|
|
- `ctx.ip`
|
|
|
|
- `ctx.ips`
|
|
|
|
- `ctx.subdomains`
|
|
|
|
- `ctx.is()`
|
|
|
|
- `ctx.accepts()`
|
|
|
|
- `ctx.acceptsEncodings()`
|
|
|
|
- `ctx.acceptsCharsets()`
|
|
|
|
- `ctx.acceptsLanguages()`
|
|
|
|
- `ctx.get()`
|
|
|
|
|
|
|
|
## Response aliases
|
|
|
|
|
|
|
|
The following accessors and alias [Response](response.md) equivalents:
|
|
|
|
|
|
|
|
- `ctx.body`
|
|
|
|
- `ctx.body=`
|
|
|
|
- `ctx.status`
|
|
|
|
- `ctx.status=`
|
|
|
|
- `ctx.length=`
|
|
|
|
- `ctx.type`
|
|
|
|
- `ctx.type=`
|
|
|
|
- `ctx.headerSent`
|
|
|
|
- `ctx.redirect()`
|
|
|
|
- `ctx.attachment()`
|
|
|
|
- `ctx.set()`
|
|
|
|
- `ctx.remove()`
|
|
|
|
- `ctx.lastModified=`
|
|
|
|
- `ctx.etag=`
|