Skip to content

Commit 08186b2

Browse files
saitolumetargos
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 1333798 commit 08186b2

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
@@ -1965,54 +1965,32 @@ Accept: text/plain\r\n
19651965
\r\n
19661966
```
19671967

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

1970-
<!-- eslint-disable semi -->
19711970
```js
1972-
'/status?name=ryan'
1971+
new URL(request.url, `http://${request.headers.host}`);
19731972
```
19741973

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

19781977
```console
19791978
$ node
1980-
> require('url').parse('/status?name=ryan')
1981-
Url {
1982-
protocol: null,
1983-
slashes: null,
1984-
auth: null,
1985-
host: null,
1986-
port: null,
1987-
hostname: null,
1988-
hash: null,
1989-
search: '?name=ryan',
1990-
query: 'name=ryan',
1979+
> new URL(request.url, request.headers.host)
1980+
URL {
1981+
href: 'http://localhost:3000/status?name=ryan',
1982+
origin: 'http://localhost:3000',
1983+
protocol: 'http:',
1984+
username: '',
1985+
password: '',
1986+
host: 'localhost:3000',
1987+
hostname: 'localhost',
1988+
port: '3000',
19911989
pathname: '/status',
1992-
path: '/status?name=ryan',
1993-
href: '/status?name=ryan' }
1994-
```
1995-
1996-
To extract the parameters from the query string, the
1997-
`require('querystring').parse` function can be used, or
1998-
`true` can be passed as the second argument to `require('url').parse`:
1999-
2000-
```console
2001-
$ node
2002-
> require('url').parse('/status?name=ryan', true)
2003-
Url {
2004-
protocol: null,
2005-
slashes: null,
2006-
auth: null,
2007-
host: null,
2008-
port: null,
2009-
hostname: null,
2010-
hash: null,
20111990
search: '?name=ryan',
2012-
query: { name: 'ryan' },
2013-
pathname: '/status',
2014-
path: '/status?name=ryan',
2015-
href: '/status?name=ryan' }
1991+
searchParams: URLSearchParams { 'name' => 'ryan' },
1992+
hash: ''
1993+
}
20161994
```
20171995

20181996
## `http.METHODS`

0 commit comments

Comments
 (0)