From 5019b5473f982c8a8d3f4de3128c5132e0405290 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 9 Jan 2023 20:15:49 +0100 Subject: [PATCH] http: res.setHeaders first implementation PR-URL: https://github.com/nodejs/node/pull/46109 Backport-PR-URL: https://github.com/nodejs/node/pull/46365 Reviewed-By: Matteo Collina Reviewed-By: Paolo Insogna Reviewed-By: Yagiz Nizipli --- doc/api/http.md | 44 ++++++ lib/_http_outgoing.js | 22 +++ .../parallel/test-http-response-setheaders.js | 131 ++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 test/parallel/test-http-response-setheaders.js 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])`