url.format(URL[, options])
URL<URL> 一个 WHATWG URL 对象options<Object>- 返回:<string>
返回 WHATWG URL 对象的 URL String 表示的可自定义序列化。
【Returns a customizable serialization of a URL String representation of a
WHATWG URL object.】
URL 对象既有返回 URL 字符串序列的 toString() 方法,也有 href 属性。然而,它们都无法进行任何自定义。url.format(URL[, options]) 方法则允许对输出进行基本的自定义。
【The URL object has both a toString() method and href property that return
string serializations of the URL. These are not, however, customizable in
any way. The url.format(URL[, options]) method allows for basic customization
of the output.】
import url from 'node:url';
const myURL = new URL('https://a:b@測試?abc#foo');
console.log(myURL.href);
// Prints https://a:b@xn--g6w251d/?abc#foo
console.log(myURL.toString());
// Prints https://a:b@xn--g6w251d/?abc#foo
console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));
// Prints 'https://測試/?abc'const url = require('node:url');
const myURL = new URL('https://a:b@測試?abc#foo');
console.log(myURL.href);
// Prints https://a:b@xn--g6w251d/?abc#foo
console.log(myURL.toString());
// Prints https://a:b@xn--g6w251d/?abc#foo
console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));
// Prints 'https://測試/?abc'