- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
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/
