
- 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 使用 Where 條件
- Node.js - MySQL 使用 Order By 排序
- 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.origin 屬性
NodeJS url.origin 屬性是 URL 類的一個只讀屬性,用於獲取 URL 來源的序列化字串。
此屬性不允許設定 URL 來源的序列化字串。如果嘗試為 URL 設定新的來源,則會被忽略,並且現有來源不會受到影響。如果為 origin 屬性分配的值不是有效的 URL,則會丟擲 TypeError。
語法
以下是NodeJS URL 類 origin 屬性的語法
URL.origin
引數
此屬性不接受任何引數。
返回值
此屬性僅獲取 URL 來源的只讀序列化字串。
以下示例演示了路徑模組中 NodeJS URL.origin 屬性的使用。
示例
如果我們將 URL 分配給 NodeJS origin 屬性,它將只返回給定 URL 來源的只讀序列化字串。
在以下示例中,我們嘗試列印輸入 URL 的來源。
const url = require('url'); const myURL = new URL("https://tutorialspoint.tw/index.htm"); console.log(myURL.origin);
輸出
如下所示,origin 屬性返回給定 URL 的來源。
https://tutorialspoint.tw
示例
如果我們嘗試使用 URL.origin 屬性設定 URL 的來源,它將不允許這樣做,然後它將被忽略。來源不會受到影響。
在以下示例中,我們嘗試使用 URL.origin 屬性修改給定 URL 的來源。
const url = require('url'); const myURL = new URL("https://tutorialspoint.tw/index.htm"); console.log("Before setting the Origin: " + myURL.origin); myURL.origin = "https://www.tutorix.com/index.htm"; console.log("After setting a new Origin to myURL: " + myURL.origin);
輸出
如下所示,origin 屬性不允許為輸入 URL 設定新的來源。
Before setting the Origin: https://tutorialspoint.tw After setting a new Origin to myURL: https://tutorialspoint.tw
示例
如果我們將不是有效 URL 型別的值分配給 origin 屬性,則 origin 屬性將丟擲 TypeError。
在給定的程式中,我們將無效的 URL 分配給 origin 屬性。
const url = require('url'); const myURL = new URL(2746832); console.log("The origin: " + myURL.origin);
TypeError
如下所示,origin 屬性丟擲了 TypeError。
node:internal/url:564 throw new ERR_INVALID_URL(input); ^ TypeError [ERR_INVALID_URL]: Invalid URL at new NodeError (node:internal/errors:387:5) at URL.onParseError (node:internal/url:564:9) at new URL (node:internal/url:640:5) at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:3:15) at Module._compile (node:internal/modules/cjs/loader:1126:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 { input: '2746832', code: 'ERR_INVALID_URL' }
示例
如果輸入 URL 的主機名中出現 Unicode 字元,則它將自動轉換為 ASCII。
在以下程式中,我們嘗試將包含 Unicode 字元的 URL 分配給 origin 屬性。
const url = require('url'); const myURL = new URL("https://おねがいします"); console.log(myURL.origin);
輸出
如果執行上述程式,則輸出如下所示。
https://xn--n8jlg9bk0j8e
nodejs_url_module.htm
廣告