GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — http: enable call chaining with setHeader() · nodejs/node@ad39b37

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • http.md
  • lib
    • _http_outgoing.js
  • test/parallel
    • test-http-set-header-chain.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/http.md‎

Copy file name to clipboardExpand all lines: doc/api/http.md
+5-1Lines changed: 5 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1587,13 +1587,17 @@ added: v0.4.0
15871587

15881588
* `name` {string}
15891589
* `value` {any}
1590+
* Returns: {http.ServerResponse}
1591+
1592+
Returns the response object.
15901593

15911594
Sets a single header value for implicit headers. If this header already exists
15921595
in the to-be-sent headers, its value will be replaced. Use an array of strings
15931596
here to send multiple headers with the same name. Non-string values will be
15941597
stored without modification. Therefore, [`response.getHeader()`][] may return
15951598
non-string values. However, the non-string values will be converted to strings
1596-
for network transmission.
1599+
for network transmission. The same response object is returned to the caller,
1600+
to enable call chaining.
15971601

15981602
```js
15991603
response.setHeader('Content-Type', 'text/html');
Collapse file

‎lib/_http_outgoing.js‎

Copy file name to clipboardExpand all lines: lib/_http_outgoing.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
565565
this[kOutHeaders] = headers = ObjectCreate(null);
566566

567567
headers[name.toLowerCase()] = [name, value];
568+
return this;
568569
};
569570

570571

Collapse file

‎test/parallel/test-http-set-header-chain.js‎

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../common');
3+
const http = require('http');
4+
const assert = require('assert');
5+
const expected = {
6+
'__proto__': null,
7+
'testheader1': 'foo',
8+
'testheader2': 'bar',
9+
'testheader3': 'xyz'
10+
};
11+
const server = http.createServer(common.mustCall((req, res) => {
12+
let retval = res.setHeader('testheader1', 'foo');
13+
14+
// Test that the setHeader returns the same response object.
15+
assert.strictEqual(retval, res);
16+
17+
retval = res.setHeader('testheader2', 'bar').setHeader('testheader3', 'xyz');
18+
// Test that chaining works for setHeader.
19+
assert.deepStrictEqual(res.getHeaders(), expected);
20+
res.end('ok');
21+
}));
22+
server.listen(0, () => {
23+
http.get({ port: server.address().port }, common.mustCall((res) => {
24+
res.on('data', () => {});
25+
res.on('end', common.mustCall(() => {
26+
server.close();
27+
}));
28+
}));
29+
});

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.