• Node.js Video Tutorials

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
nodejs_url_module.htm
廣告