ctx.set()
Sets one or more response headers on the current request; delegates to `ctx.response.set()`.
Syntax
ctx.set(field, value)
ctx.set(headers)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
field | string | No | The header name (case-insensitive). Use the two-argument form to set a single header. |
value | string | string[] | No | Header value or array of values for multi-value headers. |
headers | object | No | A plain object of `{ headerName: value }` pairs to set multiple headers at once. |
Returns
void — Returns nothing.
Examples
import Koa from 'koa';
const app = new Koa();
app.use(async (ctx) => {
ctx.set('X-Powered-By', 'Koa');
ctx.set({
'Cache-Control': 'no-store',
'X-Frame-Options': 'DENY',
});
ctx.body = 'secured';
});
app.listen(3000);
Output
X-Powered-By: Koa
Cache-Control: no-store
X-Frame-Options: DENY
Notes
`ctx.set` is an alias for `ctx.response.set`. Use `ctx.append(field, value)`
to add to an existing header rather than replacing it. Headers set before
`ctx.body` is assigned are preserved.