From e76d5bf241a27f43f74928513a2ad3c7f7d0b71a Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 2 Apr 2017 21:39:42 +0300 Subject: [PATCH 1/7] doc: replace `var` by `const` in http.md --- doc/api/http.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 88d83382ff070d..9de24c0a68de27 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -130,7 +130,7 @@ To configure any of them, a custom [`http.Agent`][] instance must be created. ```js const http = require('http'); -var keepAliveAgent = new http.Agent({ keepAlive: true }); +const keepAliveAgent = new http.Agent({ keepAlive: true }); options.agent = keepAliveAgent; http.request(options, onResponseCallback); ``` @@ -309,14 +309,14 @@ const net = require('net'); const url = require('url'); // Create an HTTP tunneling proxy -var proxy = http.createServer( (req, res) => { +const proxy = http.createServer( (req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('okay'); }); proxy.on('connect', (req, cltSocket, head) => { // connect to an origin server - var srvUrl = url.parse(`http://${req.url}`); - var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => { + const srvUrl = url.parse(`http://${req.url}`); + const srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => { cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node.js-Proxy\r\n' + '\r\n'); @@ -330,14 +330,14 @@ proxy.on('connect', (req, cltSocket, head) => { proxy.listen(1337, '127.0.0.1', () => { // make a request to a tunneling proxy - var options = { + const options = { port: 1337, hostname: '127.0.0.1', method: 'CONNECT', path: 'www.google.com:80' }; - var req = http.request(options); + const req = http.request(options); req.end(); req.on('connect', (res, socket, head) => { @@ -405,7 +405,7 @@ A client server pair demonstrating how to listen for the `'upgrade'` event. const http = require('http'); // Create an HTTP server -var srv = http.createServer( (req, res) => { +const srv = http.createServer( (req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('okay'); }); @@ -422,7 +422,7 @@ srv.on('upgrade', (req, socket, head) => { srv.listen(1337, '127.0.0.1', () => { // make a request - var options = { + const options = { port: 1337, hostname: '127.0.0.1', headers: { @@ -431,7 +431,7 @@ srv.listen(1337, '127.0.0.1', () => { } }; - var req = http.request(options); + const req = http.request(options); req.end(); req.on('upgrade', (res, socket, upgradeHead) => { @@ -944,7 +944,7 @@ Note that the name is case insensitive. Example: ```js -var contentType = response.getHeader('content-type'); +const contentType = response.getHeader('content-type'); ``` ### response.getHeaderNames() @@ -963,7 +963,7 @@ Example: response.setHeader('Foo', 'bar'); response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']); -var headerNames = response.getHeaderNames(); +const headerNames = response.getHeaderNames(); // headerNames === ['foo', 'set-cookie'] ``` @@ -986,7 +986,7 @@ Example: response.setHeader('Foo', 'bar'); response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']); -var headers = response.getHeaders(); +const headers = response.getHeaders(); // headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] } ``` @@ -1004,7 +1004,7 @@ outgoing headers. Note that the header name matching is case-insensitive. Example: ```js -var hasContentType = response.hasHeader('content-type'); +const hasContentType = response.hasHeader('content-type'); ``` ### response.headersSent @@ -1209,7 +1209,7 @@ argument. Example: ```js -var body = 'hello world'; +const body = 'hello world'; response.writeHead(200, { 'Content-Length': Buffer.byteLength(body), 'Content-Type': 'text/plain' }); @@ -1647,11 +1647,11 @@ upload a file with a POST request, then write to the `ClientRequest` object. Example: ```js -var postData = querystring.stringify({ +const postData = querystring.stringify({ 'msg' : 'Hello World!' }); -var options = { +const options = { hostname: 'www.google.com', port: 80, path: '/upload', @@ -1662,7 +1662,7 @@ var options = { } }; -var req = http.request(options, (res) => { +const req = http.request(options, (res) => { console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); res.setEncoding('utf8'); From 21c9dbd1077dcda5976e595134f51a7a2a66394a Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 2 Apr 2017 21:41:10 +0300 Subject: [PATCH 2/7] doc: replace `let` by `const` in http.md --- doc/api/http.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index 9de24c0a68de27..26a491fbeb8927 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1569,7 +1569,7 @@ http.get('http://nodejs.org/dist/index.json', (res) => { res.on('data', (chunk) => rawData += chunk); res.on('end', () => { try { - let parsedData = JSON.parse(rawData); + const parsedData = JSON.parse(rawData); console.log(parsedData); } catch (e) { console.log(e.message); From 155db174ab24a9f35e823ee8a4880113c1400031 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 2 Apr 2017 21:43:05 +0300 Subject: [PATCH 3/7] doc: fix spaces in code examples of http.md --- doc/api/http.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 26a491fbeb8927..90e1cc008bad3e 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1077,7 +1077,7 @@ any headers passed to [`response.writeHead()`][], with the headers passed to ```js // returns content-type = text/plain -const server = http.createServer((req,res) => { +const server = http.createServer((req, res) => { res.setHeader('Content-Type', 'text/html'); res.setHeader('X-Foo', 'bar'); res.writeHead(200, {'Content-Type': 'text/plain'}); @@ -1227,7 +1227,7 @@ any headers passed to [`response.writeHead()`][], with the headers passed to ```js // returns content-type = text/plain -const server = http.createServer((req,res) => { +const server = http.createServer((req, res) => { res.setHeader('Content-Type', 'text/html'); res.setHeader('X-Foo', 'bar'); res.writeHead(200, {'Content-Type': 'text/plain'}); @@ -1648,7 +1648,7 @@ Example: ```js const postData = querystring.stringify({ - 'msg' : 'Hello World!' + 'msg': 'Hello World!' }); const options = { From 930cecbd7c2c3998d2f43d2ab8955c8ed6dbc283 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 2 Apr 2017 21:50:50 +0300 Subject: [PATCH 4/7] doc: replace console.log() by .error() in http.md --- doc/api/http.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 90e1cc008bad3e..a6c978f9a9397d 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1558,7 +1558,7 @@ http.get('http://nodejs.org/dist/index.json', (res) => { `Expected application/json but received ${contentType}`); } if (error) { - console.log(error.message); + console.error(error.message); // consume response data to free up memory res.resume(); return; @@ -1572,11 +1572,11 @@ http.get('http://nodejs.org/dist/index.json', (res) => { const parsedData = JSON.parse(rawData); console.log(parsedData); } catch (e) { - console.log(e.message); + console.error(e.message); } }); }).on('error', (e) => { - console.log(`Got error: ${e.message}`); + console.error(`Got error: ${e.message}`); }); ``` @@ -1675,7 +1675,7 @@ const req = http.request(options, (res) => { }); req.on('error', (e) => { - console.log(`problem with request: ${e.message}`); + console.error(`problem with request: ${e.message}`); }); // write data to request body From 69b3c9d53a4fde873c298a5831964f8491fb2195 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 2 Apr 2017 21:55:18 +0300 Subject: [PATCH 5/7] doc: make arrow function clearer in http.md --- doc/api/http.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index a6c978f9a9397d..f42e7aa91883c9 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1566,7 +1566,7 @@ http.get('http://nodejs.org/dist/index.json', (res) => { res.setEncoding('utf8'); let rawData = ''; - res.on('data', (chunk) => rawData += chunk); + res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { try { const parsedData = JSON.parse(rawData); From 10481af6499e8ff54bbda07b0fff2d153e6df9b0 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 2 Apr 2017 21:56:10 +0300 Subject: [PATCH 6/7] doc: use object destructuring in http.md --- doc/api/http.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index f42e7aa91883c9..8f86d267f51d57 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1546,7 +1546,7 @@ JSON Fetching Example: ```js http.get('http://nodejs.org/dist/index.json', (res) => { - const statusCode = res.statusCode; + const { statusCode } = res; const contentType = res.headers['content-type']; let error; From 317d2136ce3cd5c47f5d7afd77814a20e8e9ce5a Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 2 Apr 2017 21:57:45 +0300 Subject: [PATCH 7/7] doc: update output examples in http.md --- doc/api/http.md | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 8f86d267f51d57..208dc8fa760a60 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1466,12 +1466,19 @@ can be used. Example: ```txt $ node > require('url').parse('/status?name=ryan') -{ - href: '/status?name=ryan', +Url { + protocol: null, + slashes: null, + auth: null, + host: null, + port: null, + hostname: null, + hash: null, search: '?name=ryan', query: 'name=ryan', - pathname: '/status' -} + pathname: '/status', + path: '/status?name=ryan', + href: '/status?name=ryan' } ``` To extract the parameters from the query string, the @@ -1482,12 +1489,19 @@ Example: ```txt $ node > require('url').parse('/status?name=ryan', true) -{ - href: '/status?name=ryan', +Url { + protocol: null, + slashes: null, + auth: null, + host: null, + port: null, + hostname: null, + hash: null, search: '?name=ryan', - query: {name: 'ryan'}, - pathname: '/status' -} + query: { name: 'ryan' }, + pathname: '/status', + path: '/status?name=ryan', + href: '/status?name=ryan' } ``` ## http.METHODS