Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: update message.url example in http.IncomingMessage #30830

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 17 additions & 39 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1973,54 +1973,32 @@ Accept: text/plain\r\n
\r\n
```

Then `request.url` will be:
To parse the URL into its parts:

<!-- eslint-disable semi -->
```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(request.url, request.headers.host)
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
Expand Down