From 8f76b058fca97bf259ac78b2c253048b3c128963 Mon Sep 17 00:00:00 2001 From: Tadao Iseki Date: Sat, 7 Dec 2019 13:20:20 +0900 Subject: [PATCH 1/3] doc: update message.url example in http.IncomingMessage Update message.url example to use The WHATWG URL API. This is because the old example suggests using deprecated url API. Fixes: https://github.com/nodejs/node/issues/30048 Refs: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_message_url --- doc/api/http.md | 55 ++++++++++++++----------------------------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 007bbbf5640700..679863790712d2 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1973,54 +1973,31 @@ Accept: text/plain\r\n \r\n ``` -Then `request.url` will be: +To parse the url into its parts The WHATWG URL API can be used: - ```js -'/status?name=ryan' +new URL(request.url, `http://${request.headers.host}`); ``` -To parse the url into its parts `require('url').parse(request.url)` -can be used: +When `request.url` is `'/status?name=ryan'` and `request.headers.host` is `'localhost:3000'`: ```console $ node -> require('url').parse('/status?name=ryan') -Url { - protocol: null, - slashes: null, - auth: null, - host: null, - port: null, - hostname: null, - hash: null, - search: '?name=ryan', - query: 'name=ryan', +> new URL('/status?name=ryan', 'localhost:3000') +URL { + href: 'http://localhost:3000/status?name=ryan', + origin: 'http://localhost:3000', + protocol: 'http:', + username: '', + password: '', + host: 'localhost:3000', + hostname: 'localhost', + port: '3000', pathname: '/status', - path: '/status?name=ryan', - href: '/status?name=ryan' } -``` - -To extract the parameters from the query string, the -`require('querystring').parse` function can be used, or -`true` can be passed as the second argument to `require('url').parse`: - -```console -$ node -> require('url').parse('/status?name=ryan', true) -Url { - protocol: null, - slashes: null, - auth: null, - host: null, - port: null, - hostname: null, - hash: null, search: '?name=ryan', - query: { name: 'ryan' }, - pathname: '/status', - path: '/status?name=ryan', - href: '/status?name=ryan' } + searchParams: URLSearchParams { 'name' => 'ryan' }, + hash: '' +} ``` ## http.METHODS From 3efb569478650ef4e142ca2f6e620cad37b47ba7 Mon Sep 17 00:00:00 2001 From: Tadao Iseki Date: Sun, 8 Dec 2019 09:13:28 +0900 Subject: [PATCH 2/3] doc: wrap message.url description at 80 characters Wrapped at 80 characters and refactored. Fixes: #30048 Refs: https://github.com/nodejs/node/pull/30830 --- doc/api/http.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 679863790712d2..84a2d2bdc37fa2 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1973,13 +1973,14 @@ Accept: text/plain\r\n \r\n ``` -To parse the url into its parts The WHATWG URL API can be used: +To parse the URL into its parts: ```js new URL(request.url, `http://${request.headers.host}`); ``` -When `request.url` is `'/status?name=ryan'` and `request.headers.host` is `'localhost:3000'`: +When `request.url` is `'/status?name=ryan'` and +`request.headers.host` is `'localhost:3000'`: ```console $ node From ab5561e1155db813800f392d9b6a6ba0dec8bdd9 Mon Sep 17 00:00:00 2001 From: Tadao Iseki Date: Wed, 18 Dec 2019 14:02:02 +0900 Subject: [PATCH 3/3] doc: replace specific strings with variables Co-Authored-By: Rich Trott --- 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 84a2d2bdc37fa2..fad09c65a89fef 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1984,7 +1984,7 @@ When `request.url` is `'/status?name=ryan'` and ```console $ node -> new URL('/status?name=ryan', 'localhost:3000') +> new URL(request.url, request.headers.host) URL { href: 'http://localhost:3000/status?name=ryan', origin: 'http://localhost:3000',