Skip to content

Commit 8f76b05

Browse files
committed
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: nodejs#30048 Refs: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_message_url
1 parent 6669cd1 commit 8f76b05

File tree

1 file changed

+16
-39
lines changed

1 file changed

+16
-39
lines changed

doc/api/http.md

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,54 +1973,31 @@ Accept: text/plain\r\n
19731973
\r\n
19741974
```
19751975

1976-
Then `request.url` will be:
1976+
To parse the url into its parts The WHATWG URL API can be used:
19771977

1978-
<!-- eslint-disable semi -->
19791978
```js
1980-
'/status?name=ryan'
1979+
new URL(request.url, `http://${request.headers.host}`);
19811980
```
19821981

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

19861984
```console
19871985
$ node
1988-
> require('url').parse('/status?name=ryan')
1989-
Url {
1990-
protocol: null,
1991-
slashes: null,
1992-
auth: null,
1993-
host: null,
1994-
port: null,
1995-
hostname: null,
1996-
hash: null,
1997-
search: '?name=ryan',
1998-
query: 'name=ryan',
1986+
> new URL('/status?name=ryan', 'localhost:3000')
1987+
URL {
1988+
href: 'http://localhost:3000/status?name=ryan',
1989+
origin: 'http://localhost:3000',
1990+
protocol: 'http:',
1991+
username: '',
1992+
password: '',
1993+
host: 'localhost:3000',
1994+
hostname: 'localhost',
1995+
port: '3000',
19991996
pathname: '/status',
2000-
path: '/status?name=ryan',
2001-
href: '/status?name=ryan' }
2002-
```
2003-
2004-
To extract the parameters from the query string, the
2005-
`require('querystring').parse` function can be used, or
2006-
`true` can be passed as the second argument to `require('url').parse`:
2007-
2008-
```console
2009-
$ node
2010-
> require('url').parse('/status?name=ryan', true)
2011-
Url {
2012-
protocol: null,
2013-
slashes: null,
2014-
auth: null,
2015-
host: null,
2016-
port: null,
2017-
hostname: null,
2018-
hash: null,
20191997
search: '?name=ryan',
2020-
query: { name: 'ryan' },
2021-
pathname: '/status',
2022-
path: '/status?name=ryan',
2023-
href: '/status?name=ryan' }
1998+
searchParams: URLSearchParams { 'name' => 'ryan' },
1999+
hash: ''
2000+
}
20242001
```
20252002

20262003
## http.METHODS

0 commit comments

Comments
 (0)