The Koa response object is an abstraction of Node's response object. Like the request object, it provides added functionality with its properties and methods for building out everyday HTTP servers.
The methods and properties it exposes include the following:
- response.header: This returns the response header object.
- response.headers: This returns the response header object. It is an alias of response.header.
- response.socket: This returns the request socket.
- response.status: This returns the response status code, which is 404 by default. This is in contrast to Vanilla Node, where the default status for res.statusCode is 200.
- response.status=: This is used to set the response status code to a valid HTTP numeric status code.
- response.message: This returns the response status message. By default, this is associated with the response.status:
ctx.response.status = 202;
console.log(ctx.response.message);
// => Accepted
- response.message=: This is used to set the response status message to the supplied value.
- response.length=: This is used to set the response Content-Length header value to the given value.
- response.length: This returns the Content-Length as a number when available. It can also return the length by evaluating the content from the ctx.body when possible. It returns undefined when both the Content-Length and ctx.body are unavailable.
- response.body: This returns the response body. Popularly aliased as ctx.body.
- response.body=: This sets the response body to one of the following:
- String
- Buffer
- Stream
- Object/Array
- Null
- response.get(field): This returns the value of the field header. The field comparison is case-insensitive, as shown:
ctx.body = 'Hello World';
console.log(ctx.response.get('content-length'));
// => 11
- response.set(field, value): This is used to set the response header field to a defined value as shown here:
ctx.set('Content-Language', 'en');
- response.append(field, value): This appends an additional header field with a value.
- response.set(fields): This sets several response header fields as an object as shown:
ctx.set({
'Content-Language': 'en',
'Retry-After': 120
});
- response.remove(field): This removes a header field.
- response.type: This returns the response Content-Type.
- response.type=: This sets the Content-Type via mime string or file extension.
- response.is(types...): This checks whether the response type is one of the types specified.
- response.redirect(url, [alt]): This performs a 302 permanent redirect to a specified url. The string back provides referrer support. When the referrer is not present, alt or / is used.
To alter the default 302 status, assign the status before or after the redirect call. To alter the response body, assign it after the call:
ctx.response.status = 301;
ctx.redirect('back');
ctx.body = "redirecting you to the previous page...";
- response.attachment([filename]): This sets the Content-Disposition to attachment and optionally specifies the filename. Setting the Content-Disposition to attachment readies the client to receive the response as a download.
- response.headerSent: This returns a Boolean specifying if a response header has already been sent.
- response.lastModified: This returns the Last-Modified header as a Date if it exists.
- response.lastModified=: This sets the last-modified to a UTC string. This method can either be supplied a Date object or a simple date string.
- response.etag=: This sets the ETag header to a specified value.
- response.vary(field): This is used to set the value of the Vary header to a specified field.
- response.flushHeaders(): This method flushes any set headers and begins the response body.