Skip to content

Commit a5247cc

Browse files
qubytetargos
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 9420a73 commit a5247cc

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
@@ -1444,6 +1444,10 @@ the request body should be sent. See the [`'checkContinue'`][] event on
14441444
<!-- YAML
14451445
added: v0.1.30
14461446
changes:
1447+
- version: REPLACEME
1448+
pr-url: https://github.com/nodejs/node/pull/25974
1449+
description: Return `this` from `writeHead()` to allow chaining with
1450+
`end()`.
14471451
- version: v5.11.0, v4.4.5
14481452
pr-url: https://github.com/nodejs/node/pull/6291
14491453
description: A `RangeError` is thrown if `statusCode` is not a number in
@@ -1453,17 +1457,23 @@ changes:
14531457
*`statusCode`{number}
14541458
*`statusMessage`{string}
14551459
*`headers`{Object}
1460+
* Returns:{http.ServerResponse}
14561461

14571462
Sends a response header to the request. The status code is a 3-digit HTTP
14581463
status code, like `404`. The last argument, `headers`, are the response headers.
14591464
Optionally one can give a human-readable `statusMessage` as the second
14601465
argument.
14611466

1467+
Returns a reference to the `ServerResponse`, so that calls can be chained.
1468+
14621469
```js
14631470
constbody='hello world';
1464-
response.writeHead(200,{
1465-
'Content-Length':Buffer.byteLength(body),
1466-
'Content-Type':'text/plain' });
1471+
response
1472+
.writeHead(200,{
1473+
'Content-Length':Buffer.byteLength(body),
1474+
'Content-Type':'text/plain'
1475+
})
1476+
.end(body);
14671477
```
14681478

14691479
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
@@ -268,6 +268,8 @@ function writeHead(statusCode, reason, obj){
268268
}
269269

270270
this._storeHeader(statusLine,headers);
271+
272+
returnthis;
271273
}
272274

273275
// 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+
consthttp=require('http');
4+
constassert=require('assert');
5+
6+
constserver=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+
constchunks=[];
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)