Skip to content

Commit 7815d5f

Browse files
saitolumeBridgeAR
authored andcommitted
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: #30048 PR-URL: #30830 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 0e864a3 commit 7815d5f

File tree

1 file changed

+17
-39
lines changed

1 file changed

+17
-39
lines changed

doc/api/http.md

+17-39
Original file line numberDiff line numberDiff line change
@@ -1979,54 +1979,32 @@ Accept: text/plain\r\n
19791979
\r\n
19801980
```
19811981

1982-
Then `request.url` will be:
1982+
To parse the URL into its parts:
19831983

1984-
<!-- eslint-disable semi -->
19851984
```js
1986-
'/status?name=ryan'
1985+
new URL(request.url, `http://${request.headers.host}`);
19871986
```
19881987

1989-
To parse the url into its parts `require('url').parse(request.url)`
1990-
can be used:
1988+
When `request.url` is `'/status?name=ryan'` and
1989+
`request.headers.host` is `'localhost:3000'`:
19911990

19921991
```console
19931992
$ node
1994-
> require('url').parse('/status?name=ryan')
1995-
Url {
1996-
protocol: null,
1997-
slashes: null,
1998-
auth: null,
1999-
host: null,
2000-
port: null,
2001-
hostname: null,
2002-
hash: null,
2003-
search: '?name=ryan',
2004-
query: 'name=ryan',
1993+
> new URL(request.url, request.headers.host)
1994+
URL {
1995+
href: 'http://localhost:3000/status?name=ryan',
1996+
origin: 'http://localhost:3000',
1997+
protocol: 'http:',
1998+
username: '',
1999+
password: '',
2000+
host: 'localhost:3000',
2001+
hostname: 'localhost',
2002+
port: '3000',
20052003
pathname: '/status',
2006-
path: '/status?name=ryan',
2007-
href: '/status?name=ryan' }
2008-
```
2009-
2010-
To extract the parameters from the query string, the
2011-
`require('querystring').parse` function can be used, or
2012-
`true` can be passed as the second argument to `require('url').parse`:
2013-
2014-
```console
2015-
$ node
2016-
> require('url').parse('/status?name=ryan', true)
2017-
Url {
2018-
protocol: null,
2019-
slashes: null,
2020-
auth: null,
2021-
host: null,
2022-
port: null,
2023-
hostname: null,
2024-
hash: null,
20252004
search: '?name=ryan',
2026-
query: { name: 'ryan' },
2027-
pathname: '/status',
2028-
path: '/status?name=ryan',
2029-
href: '/status?name=ryan' }
2005+
searchParams: URLSearchParams { 'name' => 'ryan' },
2006+
hash: ''
2007+
}
20302008
```
20312009

20322010
## http.METHODS

0 commit comments

Comments
 (0)