http: makes response.writeHead return the response
Fixes: https://github.com/nodejs/node/issues/25935 PR-URL: https://github.com/nodejs/node/pull/25974 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
c2d374fccc
commit
f93df51155
@ -1450,6 +1450,10 @@ the request body should be sent. See the [`'checkContinue'`][] event on
|
|||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.1.30
|
added: v0.1.30
|
||||||
changes:
|
changes:
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/25974
|
||||||
|
description: Return `this` from `writeHead()` to allow chaining with
|
||||||
|
`end()`.
|
||||||
- version: v5.11.0, v4.4.5
|
- version: v5.11.0, v4.4.5
|
||||||
pr-url: https://github.com/nodejs/node/pull/6291
|
pr-url: https://github.com/nodejs/node/pull/6291
|
||||||
description: A `RangeError` is thrown if `statusCode` is not a number in
|
description: A `RangeError` is thrown if `statusCode` is not a number in
|
||||||
@ -1459,17 +1463,23 @@ changes:
|
|||||||
* `statusCode` {number}
|
* `statusCode` {number}
|
||||||
* `statusMessage` {string}
|
* `statusMessage` {string}
|
||||||
* `headers` {Object}
|
* `headers` {Object}
|
||||||
|
* Returns: {http.ServerResponse}
|
||||||
|
|
||||||
Sends a response header to the request. The status code is a 3-digit HTTP
|
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.
|
status code, like `404`. The last argument, `headers`, are the response headers.
|
||||||
Optionally one can give a human-readable `statusMessage` as the second
|
Optionally one can give a human-readable `statusMessage` as the second
|
||||||
argument.
|
argument.
|
||||||
|
|
||||||
|
Returns a reference to the `ServerResponse`, so that calls can be chained.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const body = 'hello world';
|
const body = 'hello world';
|
||||||
response.writeHead(200, {
|
response
|
||||||
'Content-Length': Buffer.byteLength(body),
|
.writeHead(200, {
|
||||||
'Content-Type': 'text/plain' });
|
'Content-Length': Buffer.byteLength(body),
|
||||||
|
'Content-Type': 'text/plain'
|
||||||
|
})
|
||||||
|
.end(body);
|
||||||
```
|
```
|
||||||
|
|
||||||
This method must only be called once on a message and it must
|
This method must only be called once on a message and it must
|
||||||
|
@ -270,6 +270,8 @@ function writeHead(statusCode, reason, obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._storeHeader(statusLine, headers);
|
this._storeHeader(statusLine, headers);
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Docs-only deprecated: DEP0063
|
// Docs-only deprecated: DEP0063
|
||||||
|
22
test/parallel/test-http-response-writehead-returns-this.js
Normal file
22
test/parallel/test-http-response-writehead-returns-this.js
Normal file
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user