From b840f42aba3934c01c0f14008894d2fa11429192 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 10 Aug 2018 15:54:55 +0200 Subject: [PATCH] doc: clarify that new URL().port could be an empty string PR-URL: https://github.com/nodejs/node/pull/22232 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat Reviewed-By: George Adams --- doc/api/url.md | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/doc/api/url.md b/doc/api/url.md index 00c95ccb3fa8f6..8013b1ffb75462 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -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. +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 const myURL = new URL('https://example.org:8888'); console.log(myURL.port); @@ -346,19 +371,6 @@ console.log(myURL.port); // 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, such as floating-point numbers or numbers in scientific notation, are not an exception to this rule.