accepts = function (){ return this[target][name].apply(this[target], arguments); }
...
http requests which delegate to an [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
from the node `http` module.
Here is an example of checking that a requesting client supports xml.
```js
app.use(async (ctx, next) => {
ctx.assert(ctx.request.accepts('xml'), 406);
// equivalent to:
// if (!ctx.request.accepts('xml')) ctx.throw(406);
await next();
});
```
Koa provides a `Response` object as the `response` property of the `Context`.
...
acceptsCharsets = function (){ return this[target][name].apply(this[target], arguments); }
n/a
acceptsEncodings = function (){ return this[target][name].apply(this[target], arguments); }
n/a
acceptsLanguages = function (){ return this[target][name].apply(this[target], arguments); }
n/a
append = function (){ return this[target][name].apply(this[target], arguments); }
...
/**
* Append additional header `field` with value `val`.
*
* Examples:
*
* ```
* this.append('Link', ['<http://localhost/>', '<
;http://localhost:3000/>']);
* this.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
* this.append('Warning', '199 Miscellaneous warning');
* ```
*
* @param {String} field
* @param {String|Array} val
* @api public
...
function assert(value, status, msg, opts) { if (value) return; throw createError(status, msg, opts); }
...
http requests which delegate to an [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
from the node `http` module.
Here is an example of checking that a requesting client supports xml.
```js
app.use(async (ctx, next) => {
ctx.assert(ctx.request.accepts('xml'), 406);
// equivalent to:
// if (!ctx.request.accepts('xml')) ctx.throw(406);
await next();
});
```
Koa provides a `Response` object as the `response` property of the `Context`.
...
attachment = function (){ return this[target][name].apply(this[target], arguments); }
n/a
flushHeaders = function (){ return this[target][name].apply(this[target], arguments); }
...
]);
},
/**
* Flush any set headers, and begin the body
*/
flushHeaders() {
this.res.flushHeaders();
}
};
...
get = function (){ return this[target][name].apply(this[target], arguments); }
...
*
* @return {String} hostname:port
* @api public
*/
get host() {
const proxy = this.app.proxy;
let host = proxy && this.get('X-Forwarded-Host');
host = host || this.get('Host');
if (!host) return '';
return host.split(/\s*,\s*/)[0];
},
/**
* Parse the "Host" header field hostname
...
inspect() { return this.toJSON(); }
...
/**
* Context prototype.
*/
const proto = module.exports = {
/**
* util.inspect() implementation, which
* just returns the JSON output.
*
* @return {Object}
* @api public
*/
inspect() {
...
is = function (){ return this[target][name].apply(this[target], arguments); }
...
* If there is no request body, `null` is returned.
* If there is no content type, `false` is returned.
* Otherwise, it returns the first `type` that matches.
*
* Examples:
*
* // With Content-Type: text/html; charset=utf-8
* this.is('html'); // => 'html'
* this.is('text/html'); // => 'text/html'
* this.is('text/*', 'application/json'); // => 'text/html'
*
* // When Content-Type is application/json
* this.is('json', 'urlencoded'); // => 'json'
* this.is('application/json'); // => 'application/json'
* this.is('html', 'application/*'); // => 'application/json'
...
onerror(err) { // don't do anything if there is no error. // this allows you to pass `this.onerror` // to node-style callbacks. if (null == err) return; if (!(err instanceof Error)) err = new Error(`non-error thrown: ${err}`); let headerSent = false; if (this.headerSent || !this.writable) { headerSent = err.headerSent = true; } // delegate this.app.emit('error', err, this); // nothing we can do here other // than delegate to the app-level // handler and log. if (headerSent) { return; } const { res } = this; // first unset all headers if (typeof res.getHeaderNames === 'function') { res.getHeaderNames().forEach(name => res.removeHeader(name)); } else { res._headers = {}; // Node < 7.7 } // then set those specified this.set(err.headers); // force text/plain this.type = 'text'; // ENOENT support if ('ENOENT' == err.code) err.status = 404; // default to 500 if ('number' != typeof err.status || !statuses[err.status]) err.status = 500; // respond const code = statuses[err.status]; const msg = err.expose ? err.message : code; this.status = err.status; this.length = Buffer.byteLength(msg); this.res.end(msg); }
...
this.length = val.length;
return;
}
// stream
if ('function' == typeof val.pipe) {
onFinish(this.res, destroy.bind(null, val));
ensureErrorHandler(val, err => this.ctx.onerror(err));
// overwriting
if (null != original && original != val) this.remove('Content-Length');
if (setType) this.type = 'bin';
return;
}
...
redirect = function (){ return this[target][name].apply(this[target], arguments); }
...
*
* The string "back" is special-cased
* to provide Referrer support, when Referrer
* is not present `alt` or "/" is used.
*
* Examples:
*
* this.redirect('back');
* this.redirect('back', '/index.html');
* this.redirect('/login');
* this.redirect('http://google.com');
*
* @param {String} url
* @param {String} [alt]
* @api public
...
remove = function (){ return this[target][name].apply(this[target], arguments); }
...
this._body = val;
if (this.res.headersSent) return;
// no content
if (null == val) {
if (!statuses.empty[this.status]) this.status = 204;
this.remove('Content-Type');
this.remove('Content-Length');
this.remove('Transfer-Encoding');
return;
}
// set the status
if (!this._explicitStatus) this.status = 200;
...
set = function (){ return this[target][name].apply(this[target], arguments); }
...
if (typeof res.getHeaderNames === 'function') {
res.getHeaderNames().forEach(name => res.removeHeader(name));
} else {
res._headers = {}; // Node < 7.7
}
// then set those specified
this.set(err.headers);
// force text/plain
this.type = 'text';
// ENOENT support
if ('ENOENT' == err.code) err.status = 404;
...
throw() { throw createError.apply(null, arguments); }
...
Here is an example of checking that a requesting client supports xml.
```js
app.use(async (ctx, next) => {
ctx.assert(ctx.request.accepts('xml'), 406);
// equivalent to:
// if (!ctx.request.accepts('xml')) ctx.throw(406);
await next();
});
```
Koa provides a `Response` object as the `response` property of the `Context`.
Koa's `Response` object provides helpful methods for working with
http responses which delegate to a [ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse)
...
toJSON() { return { request: this.request.toJSON(), response: this.response.toJSON(), app: this.app.toJSON(), originalUrl: this.originalUrl, req: '<original node req>', res: '<original node res>', socket: '<original node socket>' }; }
...
* just returns the JSON output.
*
* @return {Object}
* @api public
*/
inspect() {
return this.toJSON();
},
/**
* Return JSON representation.
*
* Here we explicitly invoke .toJSON() on each
* object, as iteration will otherwise fail due
...
vary = function (){ return this[target][name].apply(this[target], arguments); }
n/a
accepts() { return this.accept.types.apply(this.accept, arguments); }
...
http requests which delegate to an [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
from the node `http` module.
Here is an example of checking that a requesting client supports xml.
```js
app.use(async (ctx, next) => {
ctx.assert(ctx.request.accepts('xml'), 406);
// equivalent to:
// if (!ctx.request.accepts('xml')) ctx.throw(406);
await next();
});
```
Koa provides a `Response` object as the `response` property of the `Context`.
...
acceptsCharsets() { return this.accept.charsets.apply(this.accept, arguments); }
n/a
acceptsEncodings() { return this.accept.encodings.apply(this.accept, arguments); }
n/a
acceptsLanguages() { return this.accept.languages.apply(this.accept, arguments); }
n/a
get(field) { const req = this.req; switch (field = field.toLowerCase()) { case 'referer': case 'referrer': return req.headers.referrer || req.headers.referer || ''; default: return req.headers[field] || ''; } }
...
*
* @return {String} hostname:port
* @api public
*/
get host() {
const proxy = this.app.proxy;
let host = proxy && this.get('X-Forwarded-Host');
host = host || this.get('Host');
if (!host) return '';
return host.split(/\s*,\s*/)[0];
},
/**
* Parse the "Host" header field hostname
...
inspect() { if (!this.req) return; return this.toJSON(); }
...
/**
* Context prototype.
*/
const proto = module.exports = {
/**
* util.inspect() implementation, which
* just returns the JSON output.
*
* @return {Object}
* @api public
*/
inspect() {
...
is(types) { if (!types) return typeis(this.req); if (!Array.isArray(types)) types = [].slice.call(arguments); return typeis(this.req, types); }
...
* If there is no request body, `null` is returned.
* If there is no content type, `false` is returned.
* Otherwise, it returns the first `type` that matches.
*
* Examples:
*
* // With Content-Type: text/html; charset=utf-8
* this.is('html'); // => 'html'
* this.is('text/html'); // => 'text/html'
* this.is('text/*', 'application/json'); // => 'text/html'
*
* // When Content-Type is application/json
* this.is('json', 'urlencoded'); // => 'json'
* this.is('application/json'); // => 'application/json'
* this.is('html', 'application/*'); // => 'application/json'
...
toJSON() { return only(this, [ 'method', 'url', 'header' ]); }
...
* just returns the JSON output.
*
* @return {Object}
* @api public
*/
inspect() {
return this.toJSON();
},
/**
* Return JSON representation.
*
* Here we explicitly invoke .toJSON() on each
* object, as iteration will otherwise fail due
...
append(field, val) { const prev = this.get(field); if (prev) { val = Array.isArray(prev) ? prev.concat(val) : [prev].concat(val); } return this.set(field, val); }
...
/**
* Append additional header `field` with value `val`.
*
* Examples:
*
* ```
* this.append('Link', ['<http://localhost/>', '<
;http://localhost:3000/>']);
* this.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
* this.append('Warning', '199 Miscellaneous warning');
* ```
*
* @param {String} field
* @param {String|Array} val
* @api public
...
attachment(filename) { if (filename) this.type = extname(filename); this.set('Content-Disposition', contentDisposition(filename)); }
n/a
flushHeaders() { this.res.flushHeaders(); }
...
]);
},
/**
* Flush any set headers, and begin the body
*/
flushHeaders() {
this.res.flushHeaders();
}
};
...
get(field) { return this.header[field.toLowerCase()] || ''; }
...
*
* @return {String} hostname:port
* @api public
*/
get host() {
const proxy = this.app.proxy;
let host = proxy && this.get('X-Forwarded-Host');
host = host || this.get('Host');
if (!host) return '';
return host.split(/\s*,\s*/)[0];
},
/**
* Parse the "Host" header field hostname
...
inspect() { if (!this.res) return; const o = this.toJSON(); o.body = this.body; return o; }
...
/**
* Context prototype.
*/
const proto = module.exports = {
/**
* util.inspect() implementation, which
* just returns the JSON output.
*
* @return {Object}
* @api public
*/
inspect() {
...
is(types) { const type = this.type; if (!types) return type || false; if (!Array.isArray(types)) types = [].slice.call(arguments); return typeis(type, types); }
...
* If there is no request body, `null` is returned.
* If there is no content type, `false` is returned.
* Otherwise, it returns the first `type` that matches.
*
* Examples:
*
* // With Content-Type: text/html; charset=utf-8
* this.is('html'); // => 'html'
* this.is('text/html'); // => 'text/html'
* this.is('text/*', 'application/json'); // => 'text/html'
*
* // When Content-Type is application/json
* this.is('json', 'urlencoded'); // => 'json'
* this.is('application/json'); // => 'application/json'
* this.is('html', 'application/*'); // => 'application/json'
...
redirect(url, alt) { // location if ('back' == url) url = this.ctx.get('Referrer') || alt || '/'; this.set('Location', url); // status if (!statuses.redirect[this.status]) this.status = 302; // html if (this.ctx.accepts('html')) { url = escape(url); this.type = 'text/html; charset=utf-8'; this.body = `Redirecting to <a href="${url}">${url}</a>.`; return; } // text this.type = 'text/plain; charset=utf-8'; this.body = `Redirecting to ${url}.`; }
...
*
* The string "back" is special-cased
* to provide Referrer support, when Referrer
* is not present `alt` or "/" is used.
*
* Examples:
*
* this.redirect('back');
* this.redirect('back', '/index.html');
* this.redirect('/login');
* this.redirect('http://google.com');
*
* @param {String} url
* @param {String} [alt]
* @api public
...
remove(field) { this.res.removeHeader(field); }
...
this._body = val;
if (this.res.headersSent) return;
// no content
if (null == val) {
if (!statuses.empty[this.status]) this.status = 204;
this.remove('Content-Type');
this.remove('Content-Length');
this.remove('Transfer-Encoding');
return;
}
// set the status
if (!this._explicitStatus) this.status = 200;
...
set(field, val) { if (2 == arguments.length) { if (Array.isArray(val)) val = val.map(String); else val = String(val); this.res.setHeader(field, val); } else { for (const key in field) { this.set(key, field[key]); } } }
...
if (typeof res.getHeaderNames === 'function') {
res.getHeaderNames().forEach(name => res.removeHeader(name));
} else {
res._headers = {}; // Node < 7.7
}
// then set those specified
this.set(err.headers);
// force text/plain
this.type = 'text';
// ENOENT support
if ('ENOENT' == err.code) err.status = 404;
...
toJSON() { return only(this, [ 'status', 'message', 'header' ]); }
...
* just returns the JSON output.
*
* @return {Object}
* @api public
*/
inspect() {
return this.toJSON();
},
/**
* Return JSON representation.
*
* Here we explicitly invoke .toJSON() on each
* object, as iteration will otherwise fail due
...
vary(field) { vary(this.res, field); }
n/a