diff --git a/doc/api/http.md b/doc/api/http.md index 3d66acccbb97a1..4a3675d83bd7c3 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -2950,6 +2950,48 @@ Sets a single header value. If the header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings to send multiple headers with the same name. +### `outgoingMessage.setHeaders(headers)` + + + +* `headers` {Headers|Map} +* Returns: {http.ServerResponse} + +Returns the response object. + +Sets multiple header values for implicit headers. +`headers` must be an instance of [`Headers`][] or `Map`, +if a header already exists in the to-be-sent headers, +its value will be replaced. + +```js +const headers = new Headers({ foo: 'bar' }); +response.setHeaders(headers); +``` + +or + +```js +const headers = new Map([['foo', 'bar']]); +res.setHeaders(headers); +``` + +When headers have been set with [`outgoingMessage.setHeaders()`][], +they will be merged with any headers passed to [`response.writeHead()`][], +with the headers passed to [`response.writeHead()`][] given precedence. + +```js +// Returns content-type = text/plain +const server = http.createServer((req, res) => { + const headers = new Headers({ 'Content-Type': 'text/html' }); + res.setHeaders(headers); + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('ok'); +}); +``` + ### `outgoingMessage.setTimeout(msesc[, callback])`