• Node.js Video Tutorials

NodeJS - url.port 屬性



NodeJS url.port 屬性是 URL 模組中 URL 類的一個內建應用程式程式設計介面。

此屬性設定並獲取提供的 URL 的埠部分。埠段中的值可以是數字或包含 0 到 65535(含)範圍內數字的字串。將值設定為給定協議的 URL 物件的預設埠將導致埠值變為空字串("")。

埠值可以為空字串,在這種情況下,埠取決於協議/方案。以下是協議和埠值的列表:

協議
“ftp” 21
“file”
“http” 80
“https” 443
“ws” 80
“wss” 443

將值分配給埠段後,該值將首先使用 toString() 方法轉換為字串。如果字串無效但以數字開頭,則將前導數字分配給埠值。

語法

以下是NodeJS URL 類埠屬性的語法

URL.port

引數

此屬性不接受任何引數。

返回值

此屬性獲取並設定提供的 URL 的埠部分。

示例

如果我們將完整的 URL 分配給 NodeJS url.port 屬性,它將獲取給定 URL 的埠部分。

在下面的示例中,我們嘗試從輸入 URL 獲取埠段中的值。

const http = require('url');

const myURL = new URL('https://tutorialspoint.tw:2692');
console.log("Port portion of the URL is: " + myURL.port);

輸出

執行上述程式後,port 屬性將從提供的 URL 獲取埠段。

Port portion of the URL is: 2692

示例

如果將埠值設定為 URL 物件指定協議的預設埠,則埠值將變為空字串 (' ')。

在下面的示例中,我們正在 URL 的埠段中設定 https 和 ftp 協議的預設埠值。

const http = require('url');

const myURL1 = new URL('https://tutorialspoint.tw:443');
console.log("Port portion of the URL is: " + myURL1.port);

const myURL2 = new URL('ftp://tutorialspoint.tw:21');
console.log("Port portion of the URL is: " + myURL2.port);

輸出

正如我們在下面的輸出中看到的,port 屬性將 https 和 ftp 協議的預設埠值設定為空字串。

Port portion of the URL is: ''
Port portion of the URL is: ''

示例

URL 中埠段的值只能包含數字或包含 0 到 65535 的數字的字串。

在下面的程式中,我們嘗試將數字 (4523) 設定為輸入 URL 的埠段。

const http = require('url');

const myURL = new URL('https://tutorialspoint.tw');
console.log("The URL: " + myURL);
console.log("Port portion of the URL is: " + myURL.port);

myURL.port = 4523;
console.log("Modified the value in port portion to: " + myURL.port);

console.log(myURL.href);

輸出

正如我們在下面的輸出中看到的,port 屬性允許將 4523 設定為埠段。

The URL: https://tutorialspoint.tw/
Port portion of the URL is: 

Modified the value in port portion to: 4523
https://tutorialspoint.tw:4523/

示例

如果我們嘗試在埠段中包含完全無效的埠字串,port 屬性將忽略它們。

在下面的示例中,我們嘗試將包含字母的字串設定為 URL 的埠段。

const http = require('url');

const myURL = new URL('https://tutorialspoint.tw:5678');
console.log("The URL: " + myURL);
console.log("Port portion of the URL is: " + myURL.port);

myURL.port = "abcdefg";
console.log("Trying to set the port value as: ", "abcdefg");
console.log("After modifying: " + myURL.href);

輸出

正如我們在下面的輸出中看到的,port 屬性忽略了無效的埠字串。

The URL: https://tutorialspoint.tw:5678/
Port portion of the URL is: 5678

Trying to set the port value as:  abcdefg
After modifying: https://tutorialspoint.tw:5678/

示例

如果我們嘗試將包含字母數字值的字串設定為埠段,port 屬性將把前導數字視為埠號。

在下面的示例中,我們嘗試將字母數字值設定為埠段。

const http = require('url');

const myURL = new URL('https://tutorialspoint.tw:5678');
console.log("The URL: " + myURL);
console.log("Port portion of the URL is: " + myURL.port);

myURL.port = "2345vniu";
console.log("Trying to set the port value as: ", "2345vniu");
console.log("After modifying: " + myURL.href);

輸出

如果我們編譯並執行上述程式,port 屬性將前導數字視為埠值。

The URL: https://tutorialspoint.tw:5678/
Port portion of the URL is: 5678

Trying to set the port value as:  2345vniu
After modifying: https://tutorialspoint.tw:2345/

示例

如果將包含非整數(如浮點數)的數字或字串設定為埠段,port 屬性將截斷並僅將小數點前的數字視為埠值。

在下面的示例中,我們嘗試將非整數值設定為埠段。

const http = require('url');

const myURL = new URL('https://tutorialspoint.tw:999');
console.log("The URL: " + myURL);
console.log("Port portion of the URL is: " + myURL.port);

myURL.port = "123.456";
console.log("Trying to set the port value as: ", "123.456");
console.log("After modifying: " + myURL.href);

輸出

正如我們在下面的輸出中看到的,port 屬性截斷了並且只考慮了小數點前的數字。

The URL: https://tutorialspoint.tw:999/
Port portion of the URL is: 999

Trying to set the port value as:  123.456
After modifying: https://tutorialspoint.tw:123/

示例

如果我們嘗試將超出範圍的數字設定為埠段,port 屬性將忽略它。

在下面的示例中,我們嘗試將包含超出範圍數字的字串設定為 URL 的埠段。

const http = require('url');

const myURL = new URL('https://tutorialspoint.tw:6345');
console.log("The URL: " + myURL);
console.log("Port portion of the URL is: " + myURL.port);

myURL.port = "6553556";
console.log("Trying to set the port value as: ", "6553556");
console.log("After modifying: " + myURL.href);

輸出

執行上述程式後,我們嘗試設定的值被忽略,因為它超出了可接受的範圍。

The URL: https://tutorialspoint.tw:6345/
Port portion of the URL is: 6345

Trying to set the port value as:  6553556
After modifying: https://tutorialspoint.tw:6345/
nodejs_url_module.htm
廣告
© . All rights reserved.