url.port
历史
版本 | 变化 |
---|---|
v15.0.0 | 方案"gopher" 不再特殊。 |
获取和设置 URL 的端口部分。
端口值可以是一个数字或包含在0
到65535
(包括)范围内的数字的字符串。将值设置为给定 protocol
的 URL
对象的默认端口将导致 port
值变为空字符串 ( ''
)。
端口值可以是空字符串,在这种情况下端口取决于协议/方案:
协议 | 港口 |
---|---|
"ftp" | 21 |
"file" | |
"http" | 80 |
"https" | 443 |
"ws" | 80 |
"wss" | 443 |
为端口分配值后,该值将首先使用 .toString()
转换为字符串。
如果该字符串无效但以数字开头,则将前导数字分配给 port
。如果数字超出上述范围,则将其忽略。
const myURL = new URL('https://example.org:8888');
console.log(myURL.port);
// Prints 8888
// Default ports are automatically transformed to the empty string
// (HTTPS protocol's default port is 443)
myURL.port = '443';
console.log(myURL.port);
// Prints the empty string
console.log(myURL.href);
// Prints https://example.org/
myURL.port = 1234;
console.log(myURL.port);
// Prints 1234
console.log(myURL.href);
// Prints https://example.org:1234/
// Completely invalid port strings are ignored
myURL.port = 'abcd';
console.log(myURL.port);
// Prints 1234
// Leading numbers are treated as a port number
myURL.port = '5678abcd';
console.log(myURL.port);
// Prints 5678
// Non-integers are truncated
myURL.port = 1234.5678;
console.log(myURL.port);
// Prints 1234
// Out-of-range numbers which are not represented in scientific notation
// will be ignored.
myURL.port = 1e10; // 10000000000, will be range-checked as described below
console.log(myURL.port);
// Prints 1234
包含小数点的数字,例如浮点数或科学计数法的数字,也不例外。假设它们是有效的,小数点前的前导数字将被设置为 URL 的端口:
myURL.port = 4.567e21;
console.log(myURL.port);
// Prints 4 (because it is the leading number in the string '4.567e21')
相关用法
- Node.js URL.port用法及代码示例
- Node.js URL.protocol用法及代码示例
- Node.js URL.password用法及代码示例
- Node.js URL.pathToFileURL用法及代码示例
- Node.js URL.pathname用法及代码示例
- Node.js URL.search用法及代码示例
- Node.js URL.format用法及代码示例
- Node.js URL.username用法及代码示例
- Node.js URL.hash用法及代码示例
- Node.js URL.href用法及代码示例
- Node.js URL.hostname用法及代码示例
- Node.js URL.searchParams用法及代码示例
- Node.js URL.toJSON()用法及代码示例
- Node.js URL.origin用法及代码示例
- Node.js URL.format(urlObject)用法及代码示例
- Node.js URL.fileURLToPath用法及代码示例
- Node.js URL.host用法及代码示例
- Node.js URL.createObjectURL(blob)用法及代码示例
- Node.js URL.resolve(from,to)用法及代码示例
- Node.js URLSearchParams.set(name, value)用法及代码示例
- Node.js URLSearchParams.append()用法及代码示例
- Node.js URLSearchParams.forEach()用法及代码示例
- Node.js URLSearchParams.entries()用法及代码示例
- Node.js URLSearchParams.keys()用法及代码示例
- Node.js URLSearchParams.toString()用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 URL.port。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。