doc: clarify that new URL().port could be an empty string
PR-URL: https://github.com/nodejs/node/pull/22232 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: George Adams <george.adams@uk.ibm.com>
This commit is contained in:
parent
38145344f1
commit
2ce03804a6
@ -305,6 +305,31 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and
|
|||||||
|
|
||||||
Gets and sets the port portion of the URL.
|
Gets and sets the port portion of the URL.
|
||||||
|
|
||||||
|
The port value may be a number or a string containing a number in the range
|
||||||
|
`0` to `65535` (inclusive). Setting the value to the default port of the
|
||||||
|
`URL` objects given `protocol` will result in the `port` value becoming
|
||||||
|
the empty string (`''`).
|
||||||
|
|
||||||
|
The port value can be an empty string in which case the port depends on
|
||||||
|
the protocol/scheme:
|
||||||
|
|
||||||
|
| protocol | port |
|
||||||
|
| :------- | :--- |
|
||||||
|
| "ftp" | 21 |
|
||||||
|
| "file" | |
|
||||||
|
| "gopher" | 70 |
|
||||||
|
| "http" | 80 |
|
||||||
|
| "https" | 443 |
|
||||||
|
| "ws" | 80 |
|
||||||
|
| "wss" | 443 |
|
||||||
|
|
||||||
|
Upon assigning a value to the port, the value will first be converted to a
|
||||||
|
string using `.toString()`.
|
||||||
|
|
||||||
|
If that string is invalid but it begins with a number, the leading number is
|
||||||
|
assigned to `port`.
|
||||||
|
If the number lies outside the range denoted above, it is ignored.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const myURL = new URL('https://example.org:8888');
|
const myURL = new URL('https://example.org:8888');
|
||||||
console.log(myURL.port);
|
console.log(myURL.port);
|
||||||
@ -346,19 +371,6 @@ console.log(myURL.port);
|
|||||||
// Prints 1234
|
// Prints 1234
|
||||||
```
|
```
|
||||||
|
|
||||||
The port value may be set as either a number or as a string containing a number
|
|
||||||
in the range `0` to `65535` (inclusive). Setting the value to the default port
|
|
||||||
of the `URL` objects given `protocol` will result in the `port` value becoming
|
|
||||||
the empty string (`''`).
|
|
||||||
|
|
||||||
Upon assigning a value to the port, the value will first be converted to a
|
|
||||||
string using `.toString()`.
|
|
||||||
|
|
||||||
If that string is invalid but it begins with a number, the leading number is
|
|
||||||
assigned to `port`.
|
|
||||||
Otherwise, or if the number lies outside the range denoted above,
|
|
||||||
it is ignored.
|
|
||||||
|
|
||||||
Note that numbers which contain a decimal point,
|
Note that numbers which contain a decimal point,
|
||||||
such as floating-point numbers or numbers in scientific notation,
|
such as floating-point numbers or numbers in scientific notation,
|
||||||
are not an exception to this rule.
|
are not an exception to this rule.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user