
- 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 排序
- 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 - format() 方法
NodeJS url.format() 方法 用於返回 WHATWG URL 物件表示的 URL 字串的可自定義序列化。
也可以使用 toString() 方法或 href 屬性返回序列化的 URL。但是,它們無法自定義此 URL 中存在的片段。為此,我們使用 URL.format() 方法。
語法
以下是URL 類的 NodeJS url.format() 方法的語法
url.format(URL[, options])
引數
此方法接受兩個引數。下面描述了這些引數。
URL:此引數儲存一個 WHATWG URL 物件,該物件可以透過 new URL() 類建構函式建立。
options:可選引數可以具有以下屬性集。
auth:如果序列化的 URL 字串包含使用者名稱和密碼段,則此屬性值為true。否則,此屬性值為false。預設值為true。
fragment:如果序列化的 URL 字串包含片段段,則此屬性值為true。否則,此屬性值為false。預設值為true。
search:如果序列化的 URL 字串包含搜尋查詢段,則此屬性值為true。否則,此屬性值為false。預設值為true。
unicode:如果 URL 字串的主機段中出現的任何 Unicode 字元被編碼而不是被 Punycode 編碼,則此屬性值為false。否則,此屬性值為true。預設值為false。
返回值
此方法返回表示WHATWG URL物件的 URL 字串的可自定義序列化。
示例
如果我們將true分配給“auth”屬性,則 NodeJS url.format() 方法將在序列化的 URL 中包含“使用者名稱和密碼”段,如果為false則不包含。預設值為true。
在以下示例中,我們嘗試使用 NodeJS url.format() 方法格式化 URL 的“使用者名稱和密碼”段。
const url = require('node:url'); let myURL = new URL('https://a:b@例?1=one#footer'); console.log("URL: " + myURL.href) //it will include the 'auth' segment console.log("If 'auth' is set to true: " + url.format(myURL, {auth: true})); //it will not include the 'auth' segment console.log("If 'auth' is set to false: " + url.format(myURL, {auth: false})); //By default it includes the 'auth' segment console.log("By default: " + url.format(myURL));
輸出
執行上述程式後,它將生成以下輸出
URL: https://a:b@xn--fsq/?1=one#footer If 'auth' is set to true: https://a:b@xn--fsq/?1=one#footer If 'auth' is set to false: https://xn--fsq/?1=one#footer By default: https://a:b@xn--fsq/?1=one#footer
示例
如果我們將true分配給“fragment”屬性,則 NodeJS url.format() 方法將在序列化的 URL 中包含“fragment”段,如果為false則不包含。預設值為true。
在以下示例中,我們嘗試使用 NodeJS url.format() 方法格式化 URL 中的“fragment”段。
const url = require('node:url'); let myURL = new URL('https://a:b@例?1=one#footer'); console.log("URL: " + myURL.href) //it will include the 'fragment' segment console.log("If 'fragment' is set to true: " + url.format(myURL, {fragment: true})); //it will not include the 'fragment' segment console.log("If 'fragment' is set to false: " + url.format(myURL, {fragment: false})); //By default it includes the 'fragment' segment console.log("By default: " + url.format(myURL));
輸出
執行上述程式後,它將生成以下輸出
URL: https://a:b@xn--fsq/?1=one#footer If 'fragment' is set to true: https://a:b@xn--fsq/?1=one#footer If 'fragment' is set to false: https://a:b@xn--fsq/?1=one By default: https://a:b@xn--fsq/?1=one#footer
示例
如果我們將true分配給“search”屬性,則 NodeJS url.format() 方法將在序列化的 URL 中包含“search query”段,如果為false則不包含。預設值為true。
在以下示例中,我們嘗試使用 NodeJS url.format() 方法格式化 URL 的“search”段。
const url = require('node:url'); let myURL = new URL('https://a:b@例?1=one#footer'); console.log("URL: " + myURL.href) //it will include the 'search' segment console.log("If 'search' is set to true: " + url.format(myURL, {search: true})); //it will not include the 'search' segment console.log("If 'search' is set to false: " + url.format(myURL, {search: false})); //By default it includes the 'search' segment console.log("By default: " + url.format(myURL));
輸出
執行上述程式後,它將生成以下輸出
URL: https://a:b@xn--fsq/?1=one#footer If 'search' is set to true: https://a:b@xn--fsq/?1=one#footer If 'search' is set to false: https://a:b@xn--fsq/#footer By default: https://a:b@xn--fsq/?1=one#footer
示例
如果輸入 URL 字串包含“Unicode 字元”,並且如果我們將false分配給“unicode”屬性,則 url.format() 方法將根據 Punycode 技術對其進行編碼。如果為true,則將返回 Unicode 字元。預設值為false。
在以下示例中,我們嘗試使用 url.format() 方法格式化 URL 中的“Unicode 字元”段。
const url = require('node:url'); let myURL = new URL('https://a:b@例?1=one#footer'); console.log("URL: " + myURL.href) //it will include the 'unicode' segment console.log("If 'unicode' is set to true: " + url.format(myURL, {unicode: true})); //it will not include the 'unicode' segment console.log("If 'unicode' is set to false: " + url.format(myURL, {unicode: false})); //By default it includes the 'unicode' segment console.log("By default: " + url.format(myURL));
輸出
執行上述程式後,它將生成以下輸出
URL: https://a:b@xn--fsq/?1=one#footer If 'unicode' is set to true: https://a:b@例/?1=one#footer If 'unicode' is set to false: https://a:b@xn--fsq/?1=one#footer By default: https://a:b@xn--fsq/?1=one#footer