url.format(urlObject)
-
urlObject<Object> | <string> 网址对象(由url.parse()返回或以其他方式构造)。如果是字符串,则通过将其传给url.parse()将其转换为对象。¥
urlObject<Object> | <string> A URL object (as returned byurl.parse()or constructed otherwise). If a string, it is converted to an object by passing it tourl.parse().
url.format() 方法返回从 urlObject 派生的格式化网址字符串。
¥The url.format() method returns a formatted URL string derived from
urlObject.
const url = require('node:url');
url.format({
protocol: 'https',
hostname: 'example.com',
pathname: '/some/path',
query: {
page: 1,
format: 'json',
},
});
// => 'https://example.com/some/path?page=1&format=json' 如果 urlObject 不是对象或字符串,则 url.format() 将抛出 TypeError。
¥If urlObject is not an object or a string, url.format() will throw a
TypeError.
格式化过程如下:
¥The formatting process operates as follows:
-
创建新的空字符串
result。¥A new empty string
resultis created. -
如果
urlObject.protocol是字符串,则按原样附加到result。¥If
urlObject.protocolis a string, it is appended as-is toresult. -
否则,如果
urlObject.protocol不是undefined并且不是字符串,则抛出Error。¥Otherwise, if
urlObject.protocolis notundefinedand is not a string, anErroris thrown. -
对于不以 ASCII 冒号 (
:) 字符结尾的urlObject.protocol的所有字符串值,字面字符串:将附加到result。¥For all string values of
urlObject.protocolthat do not end with an ASCII colon (:) character, the literal string:will be appended toresult. -
如果以下任一条件为真,则字面字符串
//将附加到result:¥If either of the following conditions is true, then the literal string
//will be appended toresult:-
urlObject.slashes属性为真;¥
urlObject.slashesproperty is true; -
urlObject.protocol以http、https、ftp、gopher或file开头;¥
urlObject.protocolbegins withhttp,https,ftp,gopher, orfile;
-
-
如果
urlObject.auth属性的值为真,并且urlObject.host或urlObject.hostname不是undefined,则urlObject.auth的值将被强制转换为字符串并附加到result后跟字面串@。¥If the value of the
urlObject.authproperty is truthy, and eitherurlObject.hostorurlObject.hostnameare notundefined, the value ofurlObject.authwill be coerced into a string and appended toresultfollowed by the literal string@. -
如果
urlObject.host属性为undefined,则:¥If the
urlObject.hostproperty isundefinedthen:-
如果
urlObject.hostname是字符串,则将其附加到result。¥If the
urlObject.hostnameis a string, it is appended toresult. -
否则,如果
urlObject.hostname不是undefined并且不是字符串,则抛出Error。¥Otherwise, if
urlObject.hostnameis notundefinedand is not a string, anErroris thrown. -
如果
urlObject.port属性值为真,并且urlObject.hostname不是undefined:¥If the
urlObject.portproperty value is truthy, andurlObject.hostnameis notundefined:-
字面量字符串
:附加到result,并且¥The literal string
:is appended toresult, and -
urlObject.port的值被强制转换为字符串并附加到result。¥The value of
urlObject.portis coerced to a string and appended toresult.
-
-
-
否则,如果
urlObject.host属性值为真,则将urlObject.host的值强制转换为字符串并附加到result。¥Otherwise, if the
urlObject.hostproperty value is truthy, the value ofurlObject.hostis coerced to a string and appended toresult. -
如果
urlObject.pathname属性是非空的字符串:¥If the
urlObject.pathnameproperty is a string that is not an empty string:-
如果
urlObject.pathname不以 ASCII 正斜杠 (/) 开头,则将字面字符串'/'附加到result。¥If the
urlObject.pathnamedoes not start with an ASCII forward slash (/), then the literal string'/'is appended toresult. -
urlObject.pathname的值附加到result。¥The value of
urlObject.pathnameis appended toresult.
-
-
否则,如果
urlObject.pathname不是undefined并且不是字符串,则抛出Error。¥Otherwise, if
urlObject.pathnameis notundefinedand is not a string, anErroris thrown. -
如果
urlObject.search属性是undefined并且如果urlObject.query属性是Object,则字面串?附加到result,然后是调用querystring模块的stringify()方法的输出,并传入urlObject.query的值。¥If the
urlObject.searchproperty isundefinedand if theurlObject.queryproperty is anObject, the literal string?is appended toresultfollowed by the output of calling thequerystringmodule'sstringify()method passing the value ofurlObject.query. -
否则,如果
urlObject.search是一个字符串:¥Otherwise, if
urlObject.searchis a string:-
如果
urlObject.search的值不以 ASCII 问号 (?) 字符开头,则将字面字符串?附加到result。¥If the value of
urlObject.searchdoes not start with the ASCII question mark (?) character, the literal string?is appended toresult. -
urlObject.search的值附加到result。¥The value of
urlObject.searchis appended toresult.
-
-
否则,如果
urlObject.search不是undefined并且不是字符串,则抛出Error。¥Otherwise, if
urlObject.searchis notundefinedand is not a string, anErroris thrown. -
如果
urlObject.hash属性是字符串:¥If the
urlObject.hashproperty is a string:-
如果
urlObject.hash的值不以 ASCII 散列 (#) 字符开头,则将字面字符串#附加到result。¥If the value of
urlObject.hashdoes not start with the ASCII hash (#) character, the literal string#is appended toresult. -
urlObject.hash的值附加到result。¥The value of
urlObject.hashis appended toresult.
-
-
否则,如果
urlObject.hash属性不是undefined并且不是字符串,则抛出Error。¥Otherwise, if the
urlObject.hashproperty is notundefinedand is not a string, anErroris thrown. -
result返回。¥
resultis returned.