A URL object (as returned by url.parse()
or constructed otherwise). If a string, it is converted to an object by passing it to url.parse()
.
Optional
options: URLFormatOptionsThe url.format()
method returns a formatted URL string derived from urlObject
.
import url from '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'
If urlObject
is not an object or a string, url.format()
will throw a TypeError
.
The formatting process operates as follows:
result
is created.urlObject.protocol
is a string, it is appended as-is to result
.urlObject.protocol
is not undefined
and is not a string, an Error
is thrown.urlObject.protocol
that do not end with an ASCII
colon (:
) character, the literal string :
will be appended to result
.//
will be appended to result
:
urlObject.slashes
property is true;urlObject.protocol
begins with http
, https
, ftp
, gopher
, or file
;urlObject.auth
property is truthy, and either urlObject.host
or urlObject.hostname
are not undefined
, the value of urlObject.auth
will be coerced into a string
and appended to result
followed by the literal string @
.urlObject.host
property is undefined
then:
urlObject.hostname
is a string, it is appended to result
.urlObject.hostname
is not undefined
and is not a string,
an Error
is thrown.urlObject.port
property value is truthy, and urlObject.hostname
is not undefined
:
* The literal string :
is appended to result
, and
* The value of urlObject.port
is coerced to a string and appended to result
.urlObject.host
property value is truthy, the value of urlObject.host
is coerced to a string and appended to result
.urlObject.pathname
property is a string that is not an empty string:
urlObject.pathname
does not start with an ASCII forward slash
(/
), then the literal string '/'
is appended to result
.urlObject.pathname
is appended to result
.urlObject.pathname
is not undefined
and is not a string, an Error
is thrown.urlObject.search
property is undefined
and if the urlObject.query
property is an Object
, the literal string ?
is appended to result
followed by the output of calling the
querystring
module's stringify()
method passing the value of urlObject.query
.urlObject.search
is a string:
urlObject.search
does not start with the ASCII question
mark (?
) character, the literal string ?
is appended to result
.urlObject.search
is appended to result
.urlObject.search
is not undefined
and is not a string, an Error
is thrown.urlObject.hash
property is a string:
urlObject.hash
does not start with the ASCII hash (#
)
character, the literal string #
is appended to result
.urlObject.hash
is appended to result
.urlObject.hash
property is not undefined
and is not a
string, an Error
is thrown.result
is returned.A URL object (as returned by url.parse()
or constructed otherwise). If a string, it is converted to an object by passing it to url.parse()
.
The
url.format()
method returns a formatted URL string derived fromurlObject
.If
urlObject
is not an object or a string,url.format()
will throw aTypeError
.The formatting process operates as follows:
result
is created.urlObject.protocol
is a string, it is appended as-is toresult
.urlObject.protocol
is notundefined
and is not a string, anError
is thrown.urlObject.protocol
that do not end with an ASCII colon (:
) character, the literal string:
will be appended toresult
.//
will be appended toresult
:urlObject.slashes
property is true;urlObject.protocol
begins withhttp
,https
,ftp
,gopher
, orfile
;urlObject.auth
property is truthy, and eitherurlObject.host
orurlObject.hostname
are notundefined
, the value ofurlObject.auth
will be coerced into a string and appended toresult
followed by the literal string@
.urlObject.host
property isundefined
then:urlObject.hostname
is a string, it is appended toresult
.urlObject.hostname
is notundefined
and is not a string, anError
is thrown.urlObject.port
property value is truthy, andurlObject.hostname
is notundefined
: * The literal string:
is appended toresult
, and * The value ofurlObject.port
is coerced to a string and appended toresult
.urlObject.host
property value is truthy, the value ofurlObject.host
is coerced to a string and appended toresult
.urlObject.pathname
property is a string that is not an empty string:urlObject.pathname
does not start with an ASCII forward slash (/
), then the literal string'/'
is appended toresult
.urlObject.pathname
is appended toresult
.urlObject.pathname
is notundefined
and is not a string, anError
is thrown.urlObject.search
property isundefined
and if theurlObject.query
property is anObject
, the literal string?
is appended toresult
followed by the output of calling thequerystring
module'sstringify()
method passing the value ofurlObject.query
.urlObject.search
is a string:urlObject.search
does not start with the ASCII question mark (?
) character, the literal string?
is appended toresult
.urlObject.search
is appended toresult
.urlObject.search
is notundefined
and is not a string, anError
is thrown.urlObject.hash
property is a string:urlObject.hash
does not start with the ASCII hash (#
) character, the literal string#
is appended toresult
.urlObject.hash
is appended toresult
.urlObject.hash
property is notundefined
and is not a string, anError
is thrown.result
is returned.