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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。