8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f057cc3 commit d6e9cf7Copy full SHA for d6e9cf7
doc/api/http.md
@@ -130,7 +130,7 @@ To configure any of them, a custom [`http.Agent`][] instance must be created.
130
131
```js
132
const http = require('http');
133
-var keepAliveAgent = new http.Agent({ keepAlive: true });
+const keepAliveAgent = new http.Agent({ keepAlive: true });
134
options.agent = keepAliveAgent;
135
http.request(options, onResponseCallback);
136
```
@@ -309,14 +309,14 @@ const net = require('net');
309
const url = require('url');
310
311
// Create an HTTP tunneling proxy
312
-var proxy = http.createServer( (req, res) => {
+const proxy = http.createServer( (req, res) => {
313
res.writeHead(200, {'Content-Type': 'text/plain'});
314
res.end('okay');
315
});
316
proxy.on('connect', (req, cltSocket, head) => {
317
// connect to an origin server
318
- var srvUrl = url.parse(`http://${req.url}`);
319
- var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
+ const srvUrl = url.parse(`http://${req.url}`);
+ const srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
320
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
321
'Proxy-agent: Node.js-Proxy\r\n' +
322
'\r\n');
@@ -330,14 +330,14 @@ proxy.on('connect', (req, cltSocket, head) => {
330
proxy.listen(1337, '127.0.0.1', () => {
331
332
// make a request to a tunneling proxy
333
- var options = {
+ const options = {
334
port: 1337,
335
hostname: '127.0.0.1',
336
method: 'CONNECT',
337
path: 'www.google.com:80'
338
};
339
340
- var req = http.request(options);
+ const req = http.request(options);
341
req.end();
342
343
req.on('connect', (res, socket, head) => {
@@ -405,7 +405,7 @@ A client server pair demonstrating how to listen for the `'upgrade'` event.
405
406
407
// Create an HTTP server
408
-var srv = http.createServer( (req, res) => {
+const srv = http.createServer( (req, res) => {
409
410
411
@@ -422,7 +422,7 @@ srv.on('upgrade', (req, socket, head) => {
422
srv.listen(1337, '127.0.0.1', () => {
423
424
// make a request
425
426
427
428
headers: {
@@ -431,7 +431,7 @@ srv.listen(1337, '127.0.0.1', () => {
431
}
432
433
434
435
436
437
req.on('upgrade', (res, socket, upgradeHead) => {
@@ -944,7 +944,7 @@ Note that the name is case insensitive.
944
Example:
945
946
947
-var contentType = response.getHeader('content-type');
+const contentType = response.getHeader('content-type');
948
949
950
### response.getHeaderNames()
@@ -963,7 +963,7 @@ Example:
963
response.setHeader('Foo', 'bar');
964
response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
965
966
-var headerNames = response.getHeaderNames();
+const headerNames = response.getHeaderNames();
967
// headerNames === ['foo', 'set-cookie']
968
969
@@ -986,7 +986,7 @@ Example:
986
987
988
989
-var headers = response.getHeaders();
+const headers = response.getHeaders();
990
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
991
992
@@ -1004,7 +1004,7 @@ outgoing headers. Note that the header name matching is case-insensitive.
1004
1005
1006
1007
-var hasContentType = response.hasHeader('content-type');
+const hasContentType = response.hasHeader('content-type');
1008
1009
1010
### response.headersSent
@@ -1077,7 +1077,7 @@ any headers passed to [`response.writeHead()`][], with the headers passed to
1077
1078
1079
// returns content-type = text/plain
1080
-const server = http.createServer((req,res) => {
+const server = http.createServer((req, res) => {
1081
res.setHeader('Content-Type', 'text/html');
1082
res.setHeader('X-Foo', 'bar');
1083
@@ -1209,7 +1209,7 @@ argument.
1209
1210
1211
1212
-var body = 'hello world';
+const body = 'hello world';
1213
response.writeHead(200, {
1214
'Content-Length': Buffer.byteLength(body),
1215
'Content-Type': 'text/plain' });
@@ -1227,7 +1227,7 @@ any headers passed to [`response.writeHead()`][], with the headers passed to
1227
1228
1229
1230
1231
1232
1233
@@ -1466,12 +1466,19 @@ can be used. Example:
1466
```txt
1467
$ node
1468
> require('url').parse('/status?name=ryan')
1469
-{
1470
- href: '/status?name=ryan',
+Url {
+ protocol: null,
1471
+ slashes: null,
1472
+ auth: null,
1473
+ host: null,
1474
+ port: null,
1475
+ hostname: null,
1476
+ hash: null,
1477
search: '?name=ryan',
1478
query: 'name=ryan',
- pathname: '/status'
-}
1479
+ pathname: '/status',
1480
+ path: '/status?name=ryan',
1481
+ href: '/status?name=ryan' }
1482
1483
1484
To extract the parameters from the query string, the
@@ -1482,12 +1489,19 @@ Example:
1489
1490
1491
> require('url').parse('/status?name=ryan', true)
1485
1486
1492
1493
1494
1495
1496
1497
1498
1499
1487
1500
1488
- query: {name: 'ryan'},
1501
+ query: { name: 'ryan' },
1502
1503
1504
1505
1506
1507
## http.METHODS
@@ -1546,7 +1560,7 @@ JSON Fetching Example:
1546
1560
1547
1561
1548
1562
http.get('http://nodejs.org/dist/index.json', (res) => {
1549
- const statusCode = res.statusCode;
1563
+ const { statusCode } = res;
1550
1564
const contentType = res.headers['content-type'];
1551
1565
1552
1566
let error;
@@ -1558,25 +1572,25 @@ http.get('http://nodejs.org/dist/index.json', (res) => {
1558
1572
`Expected application/json but received ${contentType}`);
1559
1573
1574
if (error) {
- console.log(error.message);
1575
+ console.error(error.message);
1576
// consume response data to free up memory
1577
res.resume();
1578
return;
1579
1580
1567
1581
res.setEncoding('utf8');
1568
1582
let rawData = '';
1569
- res.on('data', (chunk) => rawData += chunk);
1583
+ res.on('data', (chunk) => { rawData += chunk; });
1570
1584
res.on('end', () => {
1571
1585
try {
- let parsedData = JSON.parse(rawData);
1586
+ const parsedData = JSON.parse(rawData);
1587
console.log(parsedData);
1588
} catch (e) {
- console.log(e.message);
1589
+ console.error(e.message);
1590
1591
1592
}).on('error', (e) => {
- console.log(`Got error: ${e.message}`);
1593
+ console.error(`Got error: ${e.message}`);
1594
1595
1596
@@ -1647,11 +1661,11 @@ upload a file with a POST request, then write to the `ClientRequest` object.
1647
1661
1648
1662
1649
1663
1650
-var postData = querystring.stringify({
1651
- 'msg' : 'Hello World!'
1664
+const postData = querystring.stringify({
1665
+ 'msg': 'Hello World!'
1652
1666
1653
1667
1654
-var options = {
1668
+const options = {
1655
1669
hostname: 'www.google.com',
1656
1670
port: 80,
1657
1671
path: '/upload',
@@ -1662,7 +1676,7 @@ var options = {
1676
1677
1678
-var req = http.request(options, (res) => {
1679
+const req = http.request(options, (res) => {
1680
console.log(`STATUS: ${res.statusCode}`);
1681
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
1682
@@ -1675,7 +1689,7 @@ var req = http.request(options, (res) => {
1675
1689
1690
1691
req.on('error', (e) => {
- console.log(`problem with request: ${e.message}`);
1692
+ console.error(`problem with request: ${e.message}`);
1693
1694
1695
// write data to request body