diff --git a/doc/api/http.md b/doc/api/http.md index 149fc536687f37..9031f30deacade 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1446,6 +1446,10 @@ the request body should be sent. See the [`'checkContinue'`][] event on * `statusCode` {number} * `statusMessage` {string} * `headers` {Object} +* Returns: {http2.Http2ServerResponse} Sends a response header to the request. The status code is a 3-digit HTTP status code, like `404`. The last argument, `headers`, are the response headers. +Returns a reference to the `Http2ServerResponse`, so that calls can be chained. + For compatibility with [HTTP/1][], a human-readable `statusMessage` may be passed as the second argument. However, because the `statusMessage` has no meaning within HTTP/2, the argument will have no effect and a process warning diff --git a/lib/_http_server.js b/lib/_http_server.js index 5a714da6b6c428..878c4e2bfcc1cb 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -270,6 +270,8 @@ function writeHead(statusCode, reason, obj) { } this._storeHeader(statusLine, headers); + + return this; } // Docs-only deprecated: DEP0063 diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 23c4e2e0f08893..faecf2441ce4c8 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -568,10 +568,8 @@ class Http2ServerResponse extends Stream { if (this[kStream].headersSent) throw new ERR_HTTP2_HEADERS_SENT(); - // If the stream is destroyed, we return false, - // like require('http'). if (this.stream.destroyed) - return false; + return this; if (typeof statusMessage === 'string') statusMessageWarn(); @@ -596,6 +594,8 @@ class Http2ServerResponse extends Stream { state.statusCode = statusCode; this[kBeginSend](); + + return this; } write(chunk, encoding, cb) { diff --git a/test/parallel/test-http-response-writehead-returns-this.js b/test/parallel/test-http-response-writehead-returns-this.js new file mode 100644 index 00000000000000..a62c2eca03585c --- /dev/null +++ b/test/parallel/test-http-response-writehead-returns-this.js @@ -0,0 +1,22 @@ +'use strict'; +require('../common'); +const http = require('http'); +const assert = require('assert'); + +const server = http.createServer((req, res) => { + res.writeHead(200, { 'a-header': 'a-header-value' }).end('abc'); +}); + +server.listen(0, () => { + http.get({ port: server.address().port }, (res) => { + assert.strictEqual(res.headers['a-header'], 'a-header-value'); + + const chunks = []; + + res.on('data', (chunk) => chunks.push(chunk)); + res.on('end', () => { + assert.strictEqual(Buffer.concat(chunks).toString(), 'abc'); + server.close(); + }); + }); +}); diff --git a/test/parallel/test-http2-compat-serverresponse-writehead-array.js b/test/parallel/test-http2-compat-serverresponse-writehead-array.js index e024f8ab3952f0..d856165d090f46 100644 --- a/test/parallel/test-http2-compat-serverresponse-writehead-array.js +++ b/test/parallel/test-http2-compat-serverresponse-writehead-array.js @@ -12,10 +12,11 @@ const server = h2.createServer(); server.listen(0, common.mustCall(() => { const port = server.address().port; server.once('request', common.mustCall((request, response) => { - response.writeHead(200, [ + const returnVal = response.writeHead(200, [ ['foo', 'bar'], ['ABC', 123] ]); + assert.strictEqual(returnVal, response); response.end(common.mustCall(() => { server.close(); })); })); diff --git a/test/parallel/test-http2-compat-serverresponse-writehead.js b/test/parallel/test-http2-compat-serverresponse-writehead.js index 5fd787e100350c..aabcf18ad9e0cf 100644 --- a/test/parallel/test-http2-compat-serverresponse-writehead.js +++ b/test/parallel/test-http2-compat-serverresponse-writehead.js @@ -13,7 +13,11 @@ server.listen(0, common.mustCall(function() { const port = server.address().port; server.once('request', common.mustCall(function(request, response) { response.setHeader('foo-bar', 'def456'); - response.writeHead(418, { 'foo-bar': 'abc123' }); // Override + + // Override + const returnVal = response.writeHead(418, { 'foo-bar': 'abc123' }); + + assert.strictEqual(returnVal, response); common.expectsError(() => { response.writeHead(300); }, { code: 'ERR_HTTP2_HEADERS_SENT'