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