Skip to content

Commit df76c89

Browse files
Trottrichardlau
authored andcommitted
doc: recommend URL() over url.parse() in http2 doc
PR-URL: #34978 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent ca0302e commit df76c89

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

doc/api/http2.md

+14-37
Original file line numberDiff line numberDiff line change
@@ -3092,46 +3092,25 @@ Then `request.url` will be:
30923092
'/status?name=ryan'
30933093
```
30943094

3095-
To parse the url into its parts, [`url.parse(request.url)`][`url.parse()`].
3095+
To parse the url into its parts, `new URL()` can be used:
30963096

30973097
```console
30983098
$ node
3099-
> url.parse('/status?name=ryan')
3100-
Url {
3101-
protocol: null,
3102-
slashes: null,
3103-
auth: null,
3104-
host: null,
3105-
port: null,
3106-
hostname: null,
3107-
hash: null,
3108-
search: '?name=ryan',
3109-
query: 'name=ryan',
3099+
> new URL('/status?name=ryan', 'http://example.com')
3100+
URL {
3101+
href: 'http://example.com/status?name=ryan',
3102+
origin: 'http://example.com',
3103+
protocol: 'http:',
3104+
username: '',
3105+
password: '',
3106+
host: 'example.com',
3107+
hostname: 'example.com',
3108+
port: '',
31103109
pathname: '/status',
3111-
path: '/status?name=ryan',
3112-
href: '/status?name=ryan' }
3113-
```
3114-
3115-
To obtain the parameters from the query string, use the
3116-
[`querystring.parse()`][] function or pass
3117-
`true` as the second argument to [`url.parse()`][].
3118-
3119-
```console
3120-
$ node
3121-
> url.parse('/status?name=ryan', true)
3122-
Url {
3123-
protocol: null,
3124-
slashes: null,
3125-
auth: null,
3126-
host: null,
3127-
port: null,
3128-
hostname: null,
3129-
hash: null,
31303110
search: '?name=ryan',
3131-
query: { name: 'ryan' },
3132-
pathname: '/status',
3133-
path: '/status?name=ryan',
3134-
href: '/status?name=ryan' }
3111+
searchParams: URLSearchParams { 'name' => 'ryan' },
3112+
hash: ''
3113+
}
31353114
```
31363115

31373116
### Class: `http2.Http2ServerResponse`
@@ -3704,7 +3683,6 @@ following additional properties:
37043683
[`net.Socket.prototype.unref()`]: net.html#net_socket_unref
37053684
[`net.Socket`]: net.html#net_class_net_socket
37063685
[`net.connect()`]: net.html#net_net_connect
3707-
[`querystring.parse()`]: querystring.html#querystring_querystring_parse_str_sep_eq_options
37083686
[`request.socket`]: #http2_request_socket
37093687
[`request.socket.getPeerCertificate()`]: tls.html#tls_tlssocket_getpeercertificate_detailed
37103688
[`response.end()`]: #http2_response_end_data_encoding_callback
@@ -3719,6 +3697,5 @@ following additional properties:
37193697
[`tls.TLSSocket`]: tls.html#tls_class_tls_tlssocket
37203698
[`tls.connect()`]: tls.html#tls_tls_connect_options_callback
37213699
[`tls.createServer()`]: tls.html#tls_tls_createserver_options_secureconnectionlistener
3722-
[`url.parse()`]: url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
37233700
[error code]: #error_codes
37243701
[`writable.writableFinished`]: stream.html#stream_writable_writablefinished

0 commit comments

Comments
 (0)