NodeJS url.format() 方法用于返回 URL 字符串的可自定义序列化,该字符串是 WHATWG URL 对象的表示形式。
也可以使用 toString() 方法或 href 属性返回序列化的 URL。但是,他们无法自定义此 URL 中存在的段。为此,我们使用 URL.format() 方法。
语法
以下是 URL 类的 NodeJS url.format() 方法的语法
url.format(URL[, options])
参数
此方法接受两个参数。下面将对此进行描述。
- URL:此参数包含一个 WHATWG URL 对象,该对象可由新的 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 中包含 'username 和 password' 段,如果为 false,则不包括。默认值为 true。
在以下示例中,我们尝试使用 NodeJS url.format() 方法格式化 URL 的“username 和 password”段。
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));
输出
在执行上述程序时,它将生成以下输出
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));
输出
在执行上述程序时,它将生成以下输出
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 query”段。
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));
输出
在执行上述程序时,它将生成以下输出
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));
输出
在执行上述程序时,它将生成以下输出
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