url.format(URL[, options])


  • URL <URL> 一个WHATWG URL对象
  • options <Object>

    • auth <boolean> 如果序列化的URL字符串应该包含用户名和密码为true,否则为false。默认为true
    • fragment <boolean> 如果序列化的URL字符串应该包含分段为true,否则为false。默认为true
    • search <boolean> 如果序列化的URL字符串应该包含搜索查询为true,否则为false。默认为true
    • unicode <boolean> true 如果出现在URL字符串主机元素里的Unicode字符应该被直接编码而不是使用Punycode编码为true,默认为false

返回一个WHATWG URL对象的可自定义序列化的URL字符串表达。

虽然URL对象的toString()方法和href属性都可以返回URL的序列化的字符串。然而,两者都不可以被自定义。而url.format(URL[, options])方法允许输出的基本自定义。

例如:

const { URL } = require('url');
const myURL = new URL('https://a:b@你好你好?abc#foo');

console.log(myURL.href);
  // 输出 https://a:b@xn--6qqa088eba/?abc#foo

console.log(myURL.toString());
  // 输出 https://a:b@xn--6qqa088eba/?abc#foo

console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));
  // 输出 'https://你好你好/?abc'
  • URL <URL> A WHATWG URL object
  • options <Object>

    • auth <boolean> true if the serialized URL string should include the username and password, false otherwise. Default: true.
    • fragment <boolean> true if the serialized URL string should include the fragment, false otherwise. Default: true.
    • search <boolean> true if the serialized URL string should include the search query, false otherwise. Default: true.
    • unicode <boolean> true if Unicode characters appearing in the host component of the URL string should be encoded directly as opposed to being Punycode encoded. Default: false.
  • Returns: <string>

Returns a customizable serialization of a URL String representation of a WHATWG URL object.

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.

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'