Skip to content

Commit d6e9cf7

Browse files
vsemozhetbytitaloacasas
authored andcommitted
doc: fix and update examples in http.md
* replace `var` by `const` in http.md * replace `let` by `const` in http.md * fix spaces in code examples of http.md * replace console.log() by .error() in http.md * make arrow function clearer in http.md * use object destructuring in http.md * update output examples in http.md PR-URL: #12169 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent f057cc3 commit d6e9cf7

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

doc/api/http.md

+50-36
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ To configure any of them, a custom [`http.Agent`][] instance must be created.
130130

131131
```js
132132
const http = require('http');
133-
var keepAliveAgent = new http.Agent({ keepAlive: true });
133+
const keepAliveAgent = new http.Agent({ keepAlive: true });
134134
options.agent = keepAliveAgent;
135135
http.request(options, onResponseCallback);
136136
```
@@ -309,14 +309,14 @@ const net = require('net');
309309
const url = require('url');
310310

311311
// Create an HTTP tunneling proxy
312-
var proxy = http.createServer( (req, res) => {
312+
const proxy = http.createServer( (req, res) => {
313313
res.writeHead(200, {'Content-Type': 'text/plain'});
314314
res.end('okay');
315315
});
316316
proxy.on('connect', (req, cltSocket, head) => {
317317
// connect to an origin server
318-
var srvUrl = url.parse(`http://${req.url}`);
319-
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
318+
const srvUrl = url.parse(`http://${req.url}`);
319+
const srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
320320
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
321321
'Proxy-agent: Node.js-Proxy\r\n' +
322322
'\r\n');
@@ -330,14 +330,14 @@ proxy.on('connect', (req, cltSocket, head) => {
330330
proxy.listen(1337, '127.0.0.1', () => {
331331

332332
// make a request to a tunneling proxy
333-
var options = {
333+
const options = {
334334
port: 1337,
335335
hostname: '127.0.0.1',
336336
method: 'CONNECT',
337337
path: 'www.google.com:80'
338338
};
339339

340-
var req = http.request(options);
340+
const req = http.request(options);
341341
req.end();
342342

343343
req.on('connect', (res, socket, head) => {
@@ -405,7 +405,7 @@ A client server pair demonstrating how to listen for the `'upgrade'` event.
405405
const http = require('http');
406406

407407
// Create an HTTP server
408-
var srv = http.createServer( (req, res) => {
408+
const srv = http.createServer( (req, res) => {
409409
res.writeHead(200, {'Content-Type': 'text/plain'});
410410
res.end('okay');
411411
});
@@ -422,7 +422,7 @@ srv.on('upgrade', (req, socket, head) => {
422422
srv.listen(1337, '127.0.0.1', () => {
423423

424424
// make a request
425-
var options = {
425+
const options = {
426426
port: 1337,
427427
hostname: '127.0.0.1',
428428
headers: {
@@ -431,7 +431,7 @@ srv.listen(1337, '127.0.0.1', () => {
431431
}
432432
};
433433

434-
var req = http.request(options);
434+
const req = http.request(options);
435435
req.end();
436436

437437
req.on('upgrade', (res, socket, upgradeHead) => {
@@ -944,7 +944,7 @@ Note that the name is case insensitive.
944944
Example:
945945

946946
```js
947-
var contentType = response.getHeader('content-type');
947+
const contentType = response.getHeader('content-type');
948948
```
949949

950950
### response.getHeaderNames()
@@ -963,7 +963,7 @@ Example:
963963
response.setHeader('Foo', 'bar');
964964
response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
965965

966-
var headerNames = response.getHeaderNames();
966+
const headerNames = response.getHeaderNames();
967967
// headerNames === ['foo', 'set-cookie']
968968
```
969969

@@ -986,7 +986,7 @@ Example:
986986
response.setHeader('Foo', 'bar');
987987
response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
988988

989-
var headers = response.getHeaders();
989+
const headers = response.getHeaders();
990990
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
991991
```
992992

@@ -1004,7 +1004,7 @@ outgoing headers. Note that the header name matching is case-insensitive.
10041004
Example:
10051005

10061006
```js
1007-
var hasContentType = response.hasHeader('content-type');
1007+
const hasContentType = response.hasHeader('content-type');
10081008
```
10091009

10101010
### response.headersSent
@@ -1077,7 +1077,7 @@ any headers passed to [`response.writeHead()`][], with the headers passed to
10771077

10781078
```js
10791079
// returns content-type = text/plain
1080-
const server = http.createServer((req,res) => {
1080+
const server = http.createServer((req, res) => {
10811081
res.setHeader('Content-Type', 'text/html');
10821082
res.setHeader('X-Foo', 'bar');
10831083
res.writeHead(200, {'Content-Type': 'text/plain'});
@@ -1209,7 +1209,7 @@ argument.
12091209
Example:
12101210

12111211
```js
1212-
var body = 'hello world';
1212+
const body = 'hello world';
12131213
response.writeHead(200, {
12141214
'Content-Length': Buffer.byteLength(body),
12151215
'Content-Type': 'text/plain' });
@@ -1227,7 +1227,7 @@ any headers passed to [`response.writeHead()`][], with the headers passed to
12271227

12281228
```js
12291229
// returns content-type = text/plain
1230-
const server = http.createServer((req,res) => {
1230+
const server = http.createServer((req, res) => {
12311231
res.setHeader('Content-Type', 'text/html');
12321232
res.setHeader('X-Foo', 'bar');
12331233
res.writeHead(200, {'Content-Type': 'text/plain'});
@@ -1466,12 +1466,19 @@ can be used. Example:
14661466
```txt
14671467
$ node
14681468
> require('url').parse('/status?name=ryan')
1469-
{
1470-
href: '/status?name=ryan',
1469+
Url {
1470+
protocol: null,
1471+
slashes: null,
1472+
auth: null,
1473+
host: null,
1474+
port: null,
1475+
hostname: null,
1476+
hash: null,
14711477
search: '?name=ryan',
14721478
query: 'name=ryan',
1473-
pathname: '/status'
1474-
}
1479+
pathname: '/status',
1480+
path: '/status?name=ryan',
1481+
href: '/status?name=ryan' }
14751482
```
14761483

14771484
To extract the parameters from the query string, the
@@ -1482,12 +1489,19 @@ Example:
14821489
```txt
14831490
$ node
14841491
> require('url').parse('/status?name=ryan', true)
1485-
{
1486-
href: '/status?name=ryan',
1492+
Url {
1493+
protocol: null,
1494+
slashes: null,
1495+
auth: null,
1496+
host: null,
1497+
port: null,
1498+
hostname: null,
1499+
hash: null,
14871500
search: '?name=ryan',
1488-
query: {name: 'ryan'},
1489-
pathname: '/status'
1490-
}
1501+
query: { name: 'ryan' },
1502+
pathname: '/status',
1503+
path: '/status?name=ryan',
1504+
href: '/status?name=ryan' }
14911505
```
14921506

14931507
## http.METHODS
@@ -1546,7 +1560,7 @@ JSON Fetching Example:
15461560

15471561
```js
15481562
http.get('http://nodejs.org/dist/index.json', (res) => {
1549-
const statusCode = res.statusCode;
1563+
const { statusCode } = res;
15501564
const contentType = res.headers['content-type'];
15511565

15521566
let error;
@@ -1558,25 +1572,25 @@ http.get('http://nodejs.org/dist/index.json', (res) => {
15581572
`Expected application/json but received ${contentType}`);
15591573
}
15601574
if (error) {
1561-
console.log(error.message);
1575+
console.error(error.message);
15621576
// consume response data to free up memory
15631577
res.resume();
15641578
return;
15651579
}
15661580

15671581
res.setEncoding('utf8');
15681582
let rawData = '';
1569-
res.on('data', (chunk) => rawData += chunk);
1583+
res.on('data', (chunk) => { rawData += chunk; });
15701584
res.on('end', () => {
15711585
try {
1572-
let parsedData = JSON.parse(rawData);
1586+
const parsedData = JSON.parse(rawData);
15731587
console.log(parsedData);
15741588
} catch (e) {
1575-
console.log(e.message);
1589+
console.error(e.message);
15761590
}
15771591
});
15781592
}).on('error', (e) => {
1579-
console.log(`Got error: ${e.message}`);
1593+
console.error(`Got error: ${e.message}`);
15801594
});
15811595
```
15821596

@@ -1647,11 +1661,11 @@ upload a file with a POST request, then write to the `ClientRequest` object.
16471661
Example:
16481662

16491663
```js
1650-
var postData = querystring.stringify({
1651-
'msg' : 'Hello World!'
1664+
const postData = querystring.stringify({
1665+
'msg': 'Hello World!'
16521666
});
16531667

1654-
var options = {
1668+
const options = {
16551669
hostname: 'www.google.com',
16561670
port: 80,
16571671
path: '/upload',
@@ -1662,7 +1676,7 @@ var options = {
16621676
}
16631677
};
16641678

1665-
var req = http.request(options, (res) => {
1679+
const req = http.request(options, (res) => {
16661680
console.log(`STATUS: ${res.statusCode}`);
16671681
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
16681682
res.setEncoding('utf8');
@@ -1675,7 +1689,7 @@ var req = http.request(options, (res) => {
16751689
});
16761690

16771691
req.on('error', (e) => {
1678-
console.log(`problem with request: ${e.message}`);
1692+
console.error(`problem with request: ${e.message}`);
16791693
});
16801694

16811695
// write data to request body

0 commit comments

Comments
 (0)