Skip to content

Commit f93df51

Browse files
qubyteaddaleax
authored andcommitted
http: makes response.writeHead return the response
Fixes: #25935 PR-URL: #25974 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent c2d374f commit f93df51

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

doc/api/http.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,10 @@ the request body should be sent. See the [`'checkContinue'`][] event on
14501450
<!-- YAML
14511451
added: v0.1.30
14521452
changes:
1453+
- version: REPLACEME
1454+
pr-url: https://github.com/nodejs/node/pull/25974
1455+
description: Return `this` from `writeHead()` to allow chaining with
1456+
`end()`.
14531457
- version: v5.11.0, v4.4.5
14541458
pr-url: https://github.com/nodejs/node/pull/6291
14551459
description: A `RangeError` is thrown if `statusCode` is not a number in
@@ -1459,17 +1463,23 @@ changes:
14591463
* `statusCode` {number}
14601464
* `statusMessage` {string}
14611465
* `headers` {Object}
1466+
* Returns: {http.ServerResponse}
14621467

14631468
Sends a response header to the request. The status code is a 3-digit HTTP
14641469
status code, like `404`. The last argument, `headers`, are the response headers.
14651470
Optionally one can give a human-readable `statusMessage` as the second
14661471
argument.
14671472

1473+
Returns a reference to the `ServerResponse`, so that calls can be chained.
1474+
14681475
```js
14691476
const body = 'hello world';
1470-
response.writeHead(200, {
1471-
'Content-Length': Buffer.byteLength(body),
1472-
'Content-Type': 'text/plain' });
1477+
response
1478+
.writeHead(200, {
1479+
'Content-Length': Buffer.byteLength(body),
1480+
'Content-Type': 'text/plain'
1481+
})
1482+
.end(body);
14731483
```
14741484

14751485
This method must only be called once on a message and it must

lib/_http_server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ function writeHead(statusCode, reason, obj) {
270270
}
271271

272272
this._storeHeader(statusLine, headers);
273+
274+
return this;
273275
}
274276

275277
// Docs-only deprecated: DEP0063
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
require('../common');
3+
const http = require('http');
4+
const assert = require('assert');
5+
6+
const server = http.createServer((req, res) => {
7+
res.writeHead(200, { 'a-header': 'a-header-value' }).end('abc');
8+
});
9+
10+
server.listen(0, () => {
11+
http.get({ port: server.address().port }, (res) => {
12+
assert.strictEqual(res.headers['a-header'], 'a-header-value');
13+
14+
const chunks = [];
15+
16+
res.on('data', (chunk) => chunks.push(chunk));
17+
res.on('end', () => {
18+
assert.strictEqual(Buffer.concat(chunks).toString(), 'abc');
19+
server.close();
20+
});
21+
});
22+
});

0 commit comments

Comments
 (0)