res.set()
Sets one or more response header fields.
Syntax
res.set(field[, value]) / res.set(object) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
field | string | object | Yes | A header name, or an object of name/value pairs. |
value | string | string[] | No | The header value when `field` is a string. |
Returns
Response — The response object, for chaining.
Examples
app.get('/x', (req, res) => {
res.set('Cache-Control', 'no-store');
res.send('ok');
});
Output
Cache-Control: no-store
app.get('/y', (req, res) => {
res.set({
'X-Request-Id': req.id,
'X-Frame-Options': 'DENY',
});
res.json({ ok: true });
});
Notes
Alias `res.header()`. Headers must be set before the body is sent or
Express throws `ERR_HTTP_HEADERS_SENT`. `res.get('Field')` reads a
header you have set. Setting `Content-Type` is better done via
`res.type()`.