__NOTE__: don't worry too much about memorizing these strings,
if you have a typo an error will be thrown, displaying this list
so you can make a correction.
### ctx.length
Return request Content-Length as a number when present, or undefined.
### ctx.responseLength
Return response Content-Length as a number when present, or deduce
from `ctx.body` when possible, or undefined.
### ctx.body
Get response body. When `ctx.body` is `null` and `ctx.status` is still
200 it is considered a 404. This is to prevent the developer from manually
specifying `this.status = 200` on every response.
### ctx.body=
Set response body to one of the following:
-`string` written
-`Buffer` written
-`Stream` piped
-`Object` json stringified
When a Koa application is created it injects
a middleware named `respond`, which handles
each of these `ctx.body` values. The `Content-Length`
header field is set when possible, and objects are
passed through `JSON.stringify()`.
To alter the JSON response formatting use the `app.jsonSpaces`
setting, for example to compress JSON responses set:
```js
app.jsonSpaces = 0;
```
### ctx.get(field)
Get a request header field value with case-insensitive `field`.
```js
var etag = this.get('If-None-Match');
```
### ctx.set(field, value)
Set response header `field` to `value`:
```js
this.set('Cache-Control', 'no-cache');
```
### ctx.set(fields)
Set several response header `fields` with an object:
```js
this.set({
'Etag': '1234',
'Last-Modified': date
});
```
### ctx.type
Get request `Content-Type` void of parameters such as "charset".
```js
var ct = this.type;
// => "image/png"
```
### ctx.type=
Set response `Content-Type` via mime string or file extension.
```js
this.type = 'image/png';
this.type = '.png';
this.type = 'png';
```
__NOTE__: when `ctx.body` is an object the content-type is set for you.
### ctx.url
Get request URL.
### ctx.url=
Set request URL, useful for url rewrites.
### ctx.path
Get request pathname.
### ctx.path=
Set request pathname and retain query-string when present.
### ctx.query
Get parsed query-string using [qs](https://github.com/visionmedia/node-querystring). For example with the url "/shoes?page=2&sort=asc&filters[color]=blue"
`this.query` would be the following object:
```js
{
page: '2',
sort: 'asc',
filters: {
color: 'blue'
}
}
```
__NOTE__: this property returns `{}` when no query-string is present.
### ctx.query=
Set query-string to the given object.
```js
this.query = { next: '/login' };
```
### ctx.querystring
Get raw query string void of `?`.
### ctx.querystring=
Set raw query string.
### ctx.host
Get host void of port number when present. Supports `X-Forwarded-Host`
when `app.proxy` is __true__, otherwise `Host` is used.
### ctx.fresh
Check if a request cache is "fresh", aka the contents have not changed. This
method is for cache negotiation between `If-None-Match` / `ETag`, and `If-Modified-Since` and `Last-Modified`. It should be referenced after setting one or more of these response headers.
```js
this.set('ETag', '123');
// cache is ok
if (this.fresh) {
this.status = 304;
return;
}
// cache is stale
// fetch new data
this.body = yield db.find('something');
```
### ctx.stale
Inverse of `ctx.fresh`.
### ctx.protocol
Return request protocol, "https" or "http". Supports `X-Forwarded-Proto`
when `app.proxy` is __true__.
### ctx.secure
Shorthand for `this.protocol == "https"` to check if a requset was
issued via TLS.
### ctx.ip
Request remote address. Supports `X-Forwarded-For` when `app.proxy`
is __true__.
### ctx.ips
When `X-Forwarded-For` is present and `app.proxy` is enabled an array
of these ips is returned, ordered from upstream -> downstream. When disabled
an empty array is returned.
### ctx.subdomains
Return subdomains as an array.
Subdomains are the dot-separated parts of the host before the main domain of
the app. By default, the domain of the app is assumed to be the last two
parts of the host. This can be changed by setting `app.subdomainOffset`.
For example, if the domain is "tobi.ferrets.example.com":
If `app.subdomainOffset` is not set, this.subdomains is `["ferrets", "tobi"]`.
If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]`.
### ctx.is(type)
Check if the incoming request contains the `Content-Type`
header field, and it contains the give mime `type`.
```js
// With Content-Type: text/html; charset=utf-8
this.is('html');
this.is('.html');
this.is('text/html');
this.is('text/*');
// => true
// When Content-Type is application/json
this.is('json');
this.is('.json');
this.is('application/json');
this.is('application/*');
// => true
this.is('html');
// => false
```
### ctx.redirect(url, [alt])
Perform a 302 redirect to `url`.
The string "back" is special-cased
to provide Referrer support, when Referrer
is not present `alt` or "/" is used.
```js
this.redirect('back');
this.redirect('back', '/index.html');
this.redirect('/login');
this.redirect('http://google.com');
```
To alter the default status of `302` or the response
body simply re-assign after this call:
```js
this.redirect('/cart');
this.status = 301;
this.body = 'Redirecting to shopping cart';
```
### ctx.attachment([filename])
Set `Content-Disposition` to "attachment" to signal the client
to prompt for download. Optionally specify the `filename` of the
download.
### ctx.accept(types)
Check if the given `type(s)` is acceptable, returning
the best match when true, otherwise `undefined`, in which
case you should respond with 406 "Not Acceptable".
The `type` value may be one or more mime type string
such as "application/json", the extension name
such as "json", or an array `["json", "html", "text/plain"]`. When a list or array is given the _best_ match, if any is returned.
```js
// Accept: text/html
this.accepts('html');
// => "html"
// Accept: text/*, application/json
this.accepts('html');
// => "html"
this.accepts('text/html');
// => "text/html"
this.accepts('json', 'text');
// => "json"
this.accepts('application/json');
// => "application/json"
// Accept: text/*, application/json
this.accepts('image/png');
this.accepts('png');
// => undefined
// Accept: text/*;q=.5, application/json
this.accepts(['html', 'json']);
this.accepts('html', 'json');
// => "json"
```
You may call `this.accepts()` as may times as you like,