{ "source": "doc/api/url.md", "modules": [ { "textRaw": "URL", "name": "url", "stability": 2, "stabilityText": "Stable", "desc": "
The url
module provides utilities for URL resolution and parsing. It can be\naccessed using:
const url = require('url');\n
\n",
"modules": [
{
"textRaw": "URL Strings and URL Objects",
"name": "url_strings_and_url_objects",
"desc": "A URL string is a structured string containing multiple meaningful components.\nWhen parsed, a URL object is returned containing properties for each of these\ncomponents.
\nThe following details each of the components of a parsed URL. The example\n'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
is used to\nillustrate each.
┌─────────────────────────────────────────────────────────────────────────────┐\n│ href │\n├──────────┬┬───────────┬─────────────────┬───────────────────────────┬───────┤\n│ protocol ││ auth │ host │ path │ hash │\n│ ││ ├──────────┬──────┼──────────┬────────────────┤ │\n│ ││ │ hostname │ port │ pathname │ search │ │\n│ ││ │ │ │ ├─┬──────────────┤ │\n│ ││ │ │ │ │ │ query │ │\n" http: // user:pass @ host.com : 8080 /p/a/t/h ? query=string #hash "\n│ ││ │ │ │ │ │ │ │\n└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘\n(all spaces in the "" line should be ignored -- they are purely for formatting)\n
\n",
"properties": [
{
"textRaw": "urlObject.auth",
"name": "auth",
"desc": "The auth
property is the username and password portion of the URL, also\nreferred to as "userinfo". This string subset follows the protocol
and\ndouble slashes (if present) and precedes the host
component, delimited by an\nASCII "at sign" (@
). The format of the string is {username}[:{password}]
,\nwith the [:{password}]
portion being optional.
For example: 'user:pass'
The hash
property consists of the "fragment" portion of the URL including\nthe leading ASCII hash (#
) character.
For example: '#hash'
The host
property is the full lower-cased host portion of the URL, including\nthe port
if specified.
For example: 'host.com:8080'
The hostname
property is the lower-cased host name portion of the host
\ncomponent without the port
included.
For example: 'host.com'
The href
property is the full URL string that was parsed with both the\nprotocol
and host
components converted to lower-case.
For example: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
The path
property is a concatenation of the pathname
and search
\ncomponents.
For example: '/p/a/t/h?query=string'
No decoding of the path
is performed.
The pathname
property consists of the entire path section of the URL. This\nis everything following the host
(including the port
) and before the start\nof the query
or hash
components, delimited by either the ASCII question\nmark (?
) or hash (#
) characters.
For example '/p/a/t/h'
No decoding of the path string is performed.
\n" }, { "textRaw": "urlObject.port", "name": "port", "desc": "The port
property is the numeric port portion of the host
component.
For example: '8080'
The protocol
property identifies the URL's lower-cased protocol scheme.
For example: 'http:'
The query
property is either the query string without the leading ASCII\nquestion mark (?
), or an object returned by the querystring
module's\nparse()
method. Whether the query
property is a string or object is\ndetermined by the parseQueryString
argument passed to url.parse()
.
For example: 'query=string'
or {'query': 'string'}
If returned as a string, no decoding of the query string is performed. If\nreturned as an object, both keys and values are decoded.
\n" }, { "textRaw": "urlObject.search", "name": "search", "desc": "The search
property consists of the entire "query string" portion of the\nURL, including the leading ASCII question mark (?
) character.
For example: '?query=string'
No decoding of the query string is performed.
\n" }, { "textRaw": "urlObject.slashes", "name": "slashes", "desc": "The slashes
property is a boolean
with a value of true
if two ASCII\nforward-slash characters (/
) are required following the colon in the\nprotocol
.
URLs are only permitted to contain a certain range of characters. Spaces (' '
)\nand the following characters will be automatically escaped in the\nproperties of URL objects:
< > " ` \\r \\n \\t { } | \\ ^ '\n
\nFor example, the ASCII space character (' '
) is encoded as %20
. The ASCII\nforward slash (/
) character is encoded as %3C
.
The url
module provides an experimental implementation of the\nWHATWG URL Standard as an alternative to the existing url.parse()
API.
const URL = require('url').URL;\nconst myURL = new URL('https://example.org/foo');\n\nconsole.log(myURL.href); // https://example.org/foo\nconsole.log(myURL.protocol); // https:\nconsole.log(myURL.hostname); // example.org\nconsole.log(myURL.pathname); // /foo\n
\nNote: Using the delete
keyword (e.g. delete myURL.protocol
,\ndelete myURL.pathname
, etc) has no effect but will still return true
.
A comparison between this API and url.parse()
is given below. Above the URL\n'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
, properties of an\nobject returned by url.parse()
are shown. Below it are properties of a WHATWG\nURL
object.
Note: WHATWG URL's origin
property includes protocol
and host
, but not\nusername
or password
.
┌─────────────────────────────────────────────────────────────────────────────────────────┐\n│ href │\n├──────────┬──┬─────────────────────┬─────────────────┬───────────────────────────┬───────┤\n│ protocol │ │ auth │ host │ path │ hash │\n│ │ │ ├──────────┬──────┼──────────┬────────────────┤ │\n│ │ │ │ hostname │ port │ pathname │ search │ │\n│ │ │ │ │ │ ├─┬──────────────┤ │\n│ │ │ │ │ │ │ │ query │ │\n" http: // user : pass @ host.com : 8080 /p/a/t/h ? query=string #hash "\n│ │ │ │ │ hostname │ port │ │ │ │\n│ │ │ │ ├──────────┴──────┤ │ │ │\n│ protocol │ │ username │ password │ host │ │ │ │\n├──────────┴──┼──────────┴──────────┼─────────────────┤ │ │ │\n│ origin │ │ origin │ pathname │ search │ hash │\n├─────────────┴─────────────────────┴─────────────────┴──────────┴────────────────┴───────┤\n│ href │\n└─────────────────────────────────────────────────────────────────────────────────────────┘\n(all spaces in the "" line should be ignored -- they are purely for formatting)\n
\n",
"classes": [
{
"textRaw": "Class: URL",
"type": "class",
"name": "URL",
"modules": [
{
"textRaw": "Constructor: new URL(input[, base])",
"name": "constructor:_new_url(input[,_base])",
"desc": "input
{String} The input URL to parsebase
{String | URL} The base URL to resolve against if the input
is not\nabsolute.Creates a new URL
object by parsing the input
relative to the base
. If\nbase
is passed as a string, it will be parsed equivalent to new URL(base)
.
const myURL = new URL('/foo', 'https://example.org/');\n // https://example.org/foo\n
\nA TypeError
will be thrown if the input
or base
are not valid URLs. Note\nthat an effort will be made to coerce the given values into strings. For\ninstance:
const myURL = new URL({toString: () => 'https://example.org/'});\n // https://example.org/\n
\nUnicode characters appearing within the hostname of input
will be\nautomatically converted to ASCII using the Punycode algorithm.
const myURL = new URL('https://你好你好');\n // https://xn--6qqa088eba/\n
\nAdditional examples of parsed URLs may be found in the WHATWG URL Standard.
\n", "type": "module", "displayName": "Constructor: new URL(input[, base])" } ], "properties": [ { "textRaw": "`hash` {String} ", "type": "String", "name": "hash", "desc": "Gets and sets the fragment portion of the URL.
\nconst myURL = new URL('https://example.org/foo#bar');\nconsole.log(myURL.hash);\n // Prints #bar\n\nmyURL.hash = 'baz';\nconsole.log(myURL.href);\n // Prints https://example.org/foo#baz\n
\nInvalid URL characters included in the value assigned to the hash
property\nare percent-encoded. Note that the selection of which characters to\npercent-encode may vary somewhat from what the url.parse()
and\nurl.format()
methods would produce.
Gets and sets the host portion of the URL.
\nconst myURL = new URL('https://example.org:81/foo');\nconsole.log(myURL.host);\n // Prints example.org:81\n\nmyURL.host = 'example.com:82';\nconsole.log(myURL.href);\n // Prints https://example.com:82/foo\n
\nInvalid host values assigned to the host
property are ignored.
Gets and sets the hostname portion of the URL. The key difference between\nurl.host
and url.hostname
is that url.hostname
does not include the\nport.
const myURL = new URL('https://example.org:81/foo');\nconsole.log(myURL.hostname);\n // Prints example.org\n\nmyURL.hostname = 'example.com:82';\nconsole.log(myURL.href);\n // Prints https://example.com:81/foo\n
\nInvalid hostname values assigned to the hostname
property are ignored.
Gets and sets the serialized URL.
\nconst myURL = new URL('https://example.org/foo');\nconsole.log(myURL.href);\n // Prints https://example.org/foo\n\nmyURL.href = 'https://example.com/bar'\n // Prints https://example.com/bar\n
\nGetting the value of the href
property is equivalent to calling\nurl.toString()
.
Setting the value of this property to a new value is equivalent to creating a\nnew URL
object using new URL(value)
. Each of the URL
\nobject's properties will be modified.
If the value assigned to the href
property is not a valid URL, a TypeError
\nwill be thrown.
Gets the read-only serialization of the URL's origin. Unicode characters that\nmay be contained within the hostname will be encoded as-is without Punycode\nencoding.
\nconst myURL = new URL('https://example.org/foo/bar?baz');\nconsole.log(myURL.origin);\n // Prints https://example.org\n
\nconst idnURL = new URL('https://你好你好');\nconsole.log(idnURL.origin);\n // Prints https://你好你好\n\nconsole.log(idnURL.hostname);\n // Prints xn--6qqa088eba\n
\n"
},
{
"textRaw": "`password` {String} ",
"type": "String",
"name": "password",
"desc": "Gets and sets the password portion of the URL.
\nconst myURL = new URL('https://abc:xyz@example.com');\nconsole.log(myURL.password);\n // Prints xyz\n\nmyURL.password = '123';\nconsole.log(myURL.href);\n // Prints https://abc:123@example.com\n
\nInvalid URL characters included in the value assigned to the password
property\nare percent-encoded. Note that the selection of which characters to\npercent-encode may vary somewhat from what the url.parse()
and\nurl.format()
methods would produce.
Gets and sets the path portion of the URL.
\nconst myURL = new URL('https://example.org/abc/xyz?123');\nconsole.log(myURL.pathname);\n // Prints /abc/xyz\n\nmyURL.pathname = '/abcdef';\nconsole.log(myURL.href);\n // Prints https://example.org/abcdef?123\n
\nInvalid URL characters included in the value assigned to the pathname
\nproperty are percent-encoded. Note that the selection of which characters\nto percent-encode may vary somewhat from what the url.parse()
and\nurl.format()
methods would produce.
Gets and sets the port portion of the URL.
\nconst myURL = new URL('https://example.org:8888');\nconsole.log(myURL.port);\n // Prints 8888\n\n// Default ports are automatically transformed to the empty string\n// (HTTPS protocol's default port is 443)\nmyURL.port = '443';\nconsole.log(myURL.port);\n // Prints the empty string\nconsole.log(myURL.href);\n // Prints https://example.org/\n\nmyURL.port = 1234;\nconsole.log(myURL.port);\n // Prints 1234\nconsole.log(myURL.href);\n // Prints https://example.org:1234/\n\n// Completely invalid port strings are ignored\nmyURL.port = 'abcd';\nconsole.log(myURL.port);\n // Prints 1234\n\n// Leading numbers are treated as a port number\nmyURL.port = '5678abcd';\nconsole.log(myURL.port);\n // Prints 5678\n\n// Non-integers are truncated\nmyURL.port = 1234.5678;\nconsole.log(myURL.port);\n // Prints 1234\n\n// Out-of-range numbers are ignored\nmyURL.port = 1e10;\nconsole.log(myURL.port);\n // Prints 1234\n
\nThe port value may be set as either a number or as a String containing a number\nin the range 0
to 65535
(inclusive). Setting the value to the default port\nof the URL
objects given protocol
will result in the port
value becoming\nthe empty string (''
).
If an invalid string is assigned to the port
property, but it begins with a\nnumber, the leading number is assigned to port
. Otherwise, or if the number\nlies outside the range denoted above, it is ignored.
Gets and sets the protocol portion of the URL.
\nconst myURL = new URL('https://example.org');\nconsole.log(myURL.protocol);\n // Prints http:\n\nmyURL.protocol = 'ftp';\nconsole.log(myURL.href);\n // Prints ftp://example.org\n
\nInvalid URL protocol values assigned to the protocol
property are ignored.
Gets and sets the serialized query portion of the URL.
\nconst myURL = new URL('https://example.org/abc?123');\nconsole.log(myURL.search);\n // Prints ?123\n\nmyURL.search = 'abc=xyz';\nconsole.log(myURL.href);\n // Prints https://example.org/abc?abc=xyz\n
\nAny invalid URL characters appearing in the value assigned the search
\nproperty will be percent-encoded. Note that the selection of which\ncharacters to percent-encode may vary somewhat from what the url.parse()
\nand url.format()
methods would produce.
Gets the URLSearchParams
object representing the query parameters of the\nURL. This property is read-only; to replace the entirety of query parameters of\nthe URL, use the url.search
setter. See URLSearchParams
\ndocumentation for details.
Gets and sets the username portion of the URL.
\nconst myURL = new URL('https://abc:xyz@example.com');\nconsole.log(myURL.username);\n // Prints abc\n\nmyURL.username = '123';\nconsole.log(myURL.href);\n // Prints https://123:xyz@example.com\n
\nAny invalid URL characters appearing in the value assigned the username
\nproperty will be percent-encoded. Note that the selection of which\ncharacters to percent-encode may vary somewhat from what the url.parse()
\nand url.format()
methods would produce.
The toString()
method on the URL
object returns the serialized URL. The\nvalue returned is equivalent to that of url.href
and url.toJSON()
.
Because of the need for standard compliance, this method does not allow users\nto customize the serialization process of the URL. For more flexibility,\nrequire('url').format()
method might be of interest.
The toJSON()
method on the URL
object returns the serialized URL. The\nvalue returned is equivalent to that of url.href
and\nurl.toString()
.
This method is automatically called when an URL
object is serialized\nwith JSON.stringify()
.
const myURLs = [\n new URL('https://www.example.com'),\n new URL('https://test.example.org')\n];\nconsole.log(JSON.stringify(myURLs));\n // Prints ["https://www.example.com/","https://test.example.org/"]\n
\n"
}
]
},
{
"textRaw": "Class: URLSearchParams",
"type": "class",
"name": "URLSearchParams",
"desc": "The URLSearchParams
API provides read and write access to the query of a\nURL
.
The WHATWG URLSearchParams
interface and the querystring
module have\nsimilar purpose, but the purpose of the querystring
module is more\ngeneral, as it allows the customization of delimiter characters (&
and =
).\nOn the other hand, this API is designed purely for URL query strings.
const URL = require('url').URL;\nconst myURL = new URL('https://example.org/?abc=123');\nconsole.log(myURL.searchParams.get('abc'));\n // Prints 123\n\nmyURL.searchParams.append('abc', 'xyz');\nconsole.log(myURL.href);\n // Prints https://example.org/?abc=123&abc=xyz\n\nmyURL.searchParams.delete('abc');\nmyURL.searchParams.set('a', 'b');\nconsole.log(myURL.href);\n // Prints https://example.org/?a=b\n
\n",
"methods": [
{
"textRaw": "Constructor: new URLSearchParams([init])",
"name": "Constructor: new URLSearchParams([init])",
"desc": "init
{String} The URL queryAppend a new name-value pair to the query string.
\n" }, { "textRaw": "urlSearchParams.delete(name)", "type": "method", "name": "delete", "signatures": [ { "params": [ { "textRaw": "`name` {String} ", "name": "name", "type": "String" } ] }, { "params": [ { "name": "name" } ] } ], "desc": "Remove all name-value pairs whose name is name
.
Returns an ES6 Iterator over each of the name-value pairs in the query.\nEach item of the iterator is a JavaScript Array. The first item of the Array\nis the name
, the second item of the Array is the value
.
Alias for urlSearchParams[@@iterator]()
.
Iterates over each name-value pair in the query and invokes the given function.
\nconst URL = require('url').URL;\nconst myURL = new URL('https://example.org/?a=b&c=d');\nmyURL.searchParams.forEach((value, name, searchParams) => {\n console.log(name, value, myURL.searchParams === searchParams);\n});\n // Prints:\n // a b true\n // c d true\n
\n"
},
{
"textRaw": "urlSearchParams.get(name)",
"type": "method",
"name": "get",
"signatures": [
{
"return": {
"textRaw": "Returns: {String | Null} ",
"name": "return",
"type": "String | Null"
},
"params": [
{
"textRaw": "`name` {String} ",
"name": "name",
"type": "String"
}
]
},
{
"params": [
{
"name": "name"
}
]
}
],
"desc": "Returns the value of the first name-value pair whose name is name
. If there\nare no such pairs, null
is returned.
Returns the values of all name-value pairs whose name is name
. If there are\nno such pairs, an empty array is returned.
Returns true
if there is at least one name-value pair whose name is name
.
Returns an ES6 Iterator over the names of each name-value pair.
\nconst { URLSearchParams } = require('url');\nconst params = new URLSearchParams('foo=bar&foo=baz');\nfor (const name of params.keys()) {\n console.log(name);\n}\n // Prints:\n // foo\n // foo\n
\n"
},
{
"textRaw": "urlSearchParams.set(name, value)",
"type": "method",
"name": "set",
"signatures": [
{
"params": [
{
"textRaw": "`name` {String} ",
"name": "name",
"type": "String"
},
{
"textRaw": "`value` {String} ",
"name": "value",
"type": "String"
}
]
},
{
"params": [
{
"name": "name"
},
{
"name": "value"
}
]
}
],
"desc": "Sets the value in the URLSearchParams
object associated with name
to\nvalue
. If there are any pre-existing name-value pairs whose names are name
,\nset the first such pair's value to value
and remove all others. If not,\nappend the name-value pair to the query string.
const { URLSearchParams } = require('url');\n\nconst params = new URLSearchParams();\nparams.append('foo', 'bar');\nparams.append('foo', 'baz');\nparams.append('abc', 'def');\nconsole.log(params.toString());\n // Prints foo=bar&foo=baz&abc=def\n\nparams.set('foo', 'def');\nparams.set('xyz', 'opq');\nconsole.log(params.toString());\n // Prints foo=def&abc=def&xyz=opq\n
\n"
},
{
"textRaw": "urlSearchParams.sort()",
"type": "method",
"name": "sort",
"desc": "Sort all existing name-value pairs in-place by their names. Sorting is done\nwith a stable sorting algorithm, so relative order between name-value pairs\nwith the same name is preserved.
\nThis method can be used, in particular, to increase cache hits.
\nconst params = new URLSearchParams('query[]=abc&type=search&query[]=123');\nparams.sort();\nconsole.log(params.toString());\n // Prints query%5B%5D=abc&query%5B%5D=123&type=search\n
\n",
"signatures": [
{
"params": []
}
]
},
{
"textRaw": "urlSearchParams.toString()",
"type": "method",
"name": "toString",
"signatures": [
{
"return": {
"textRaw": "Returns: {String} ",
"name": "return",
"type": "String"
},
"params": []
},
{
"params": []
}
],
"desc": "Returns the search parameters serialized as a string, with characters\npercent-encoded where necessary.
\n" }, { "textRaw": "urlSearchParams.values()", "type": "method", "name": "values", "signatures": [ { "return": { "textRaw": "Returns: {Iterator} ", "name": "return", "type": "Iterator" }, "params": [] }, { "params": [] } ], "desc": "Returns an ES6 Iterator over the values of each name-value pair.
\n" }, { "textRaw": "urlSearchParams\\[@@iterator\\]()", "type": "method", "name": "urlSearchParams\\[@@iterator\\]", "signatures": [ { "return": { "textRaw": "Returns: {Iterator} ", "name": "return", "type": "Iterator" }, "params": [] }, { "params": [] } ], "desc": "Returns an ES6 Iterator over each of the name-value pairs in the query string.\nEach item of the iterator is a JavaScript Array. The first item of the Array\nis the name
, the second item of the Array is the value
.
Alias for urlSearchParams.entries()
.
const { URLSearchParams } = require('url');\nconst params = new URLSearchParams('foo=bar&xyz=baz');\nfor (const [name, value] of params) {\n console.log(name, value);\n}\n // Prints:\n // foo bar\n // xyz baz\n
\n"
}
]
}
],
"methods": [
{
"textRaw": "require('url').domainToAscii(domain)",
"type": "method",
"name": "domainToAscii",
"signatures": [
{
"return": {
"textRaw": "Returns: {String} ",
"name": "return",
"type": "String"
},
"params": [
{
"textRaw": "`domain` {String} ",
"name": "domain",
"type": "String"
}
]
},
{
"params": [
{
"name": "'url').domainToAscii(domain"
}
]
}
],
"desc": "Returns the Punycode ASCII serialization of the domain
.
Note: The require('url').domainToAscii()
method is introduced as part of\nthe new URL
implementation but is not part of the WHATWG URL standard.
Returns the Unicode serialization of the domain
.
Note: The require('url').domainToUnicode()
API is introduced as part of the\nthe new URL
implementation but is not part of the WHATWG URL standard.
URLs are permitted to only contain a certain range of characters. Any character\nfalling outside of that range must be encoded. How such characters are encoded,\nand which characters to encode depends entirely on where the character is\nlocated within the structure of the URL. The WHATWG URL Standard uses a more\nselective and fine grained approach to selecting encoded characters than that\nused by the older url.parse()
and url.format()
methods.
The WHATWG algorithm defines three "encoding sets" that describe ranges of\ncharacters that must be percent-encoded:
\nThe simple encode set includes code points in range U+0000 to U+001F\n(inclusive) and all code points greater than U+007E.
\nThe default encode set includes the simple encode set and code points\nU+0020, U+0022, U+0023, U+003C, U+003E, U+003F, U+0060, U+007B, and U+007D.
\nThe userinfo encode set includes the default encode set and code points\nU+002F, U+003A, U+003B, U+003D, U+0040, U+005B, U+005C, U+005D, U+005E, and\nU+007C.
\nThe simple encode set is used primary for URL fragments and certain specific\nconditions for the path. The userinfo encode set is used specifically for\nusername and passwords encoded within the URL. The default encode set is used\nfor all other cases.
\nWhen non-ASCII characters appear within a hostname, the hostname is encoded\nusing the Punycode algorithm. Note, however, that a hostname may contain\nboth Punycode encoded and percent-encoded characters. For example:
\nconst URL = require('url').URL;\nconst myURL = new URL('https://%CF%80.com/foo');\nconsole.log(myURL.href);\n // Prints https://xn--1xa.com/foo\nconsole.log(myURL.origin);\n // Prints https://π.com\n
\n",
"type": "module",
"displayName": "Percent-Encoding in the WHATWG URL Standard"
}
],
"type": "module",
"displayName": "The WHATWG URL API"
}
],
"methods": [
{
"textRaw": "url.format(urlObject)",
"type": "method",
"name": "format",
"meta": {
"added": [
"v0.1.25"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`urlObject` {Object | String} 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()`. ",
"name": "urlObject",
"type": "Object | String",
"desc": "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()`."
}
]
},
{
"params": [
{
"name": "urlObject"
}
]
}
],
"desc": "The url.format()
method returns a formatted URL string derived from\nurlObject
.
If urlObject
is not an object or a string, url.parse()
will throw a\nTypeError
.
The formatting process operates as follows:
\nresult
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\nError
is thrown.urlObject.protocol
that do not end with an ASCII\ncolon (:
) character, the literal string :
will be appended to result
.//
\nwill be appended to result
:urlObject.slashes
property is true;urlObject.protocol
begins with http
, https
, ftp
, gopher
, or\nfile
;urlObject.auth
property is truthy, and either\nurlObject.host
or urlObject.hostname
are not undefined
, the value of\nurlObject.auth
will be coerced into a string and appended to result
\n 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,\nan Error
is thrown.urlObject.port
property value is truthy, and urlObject.hostname
\nis not undefined
::
is appended to result
, andurlObject.port
is coerced to a string and appended to\nresult
.urlObject.host
property value is truthy, the value of\nurlObject.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\n(/
), 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\nError
is thrown.urlObject.search
property is undefined
and if the urlObject.query
\nproperty is an Object
, the literal string ?
is appended to result
\nfollowed by the output of calling the querystring
module's stringify()
\nmethod passing the value of urlObject.query
.urlObject.search
is a string:urlObject.search
does not start with the ASCII question\nmark (?
) 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\nError
is thrown.urlObject.hash
property is a string:urlObject.hash
does not start with the ASCII hash (#
)\ncharacter, the literal string #
is appended to result
.urlObject.hash
is appended to result
.urlObject.hash
property is not undefined
and is not a\nstring, an Error
is thrown.result
is returned.Returns a customizable serialization of a URL String representation of a\nWHATWG URL object.
\nThe URL object has both a toString()
method and href
property that return\nstring serializations of the URL. These are not, however, customizable in\nany way. The url.format(URL[, options])
method allows for basic customization\nof the output.
For example:
\nconst myURL = new URL('https://a:b@你好你好?abc#foo');\n\nconsole.log(myURL.href);\n // Prints https://a:b@xn--6qqa088eba/?abc#foo\n\nconsole.log(myURL.toString());\n // Prints https://a:b@xn--6qqa088eba/?abc#foo\n\nconsole.log(url.format(myURL, {fragment: false, unicode: true, auth: false}));\n // Prints 'https://你好你好?abc'\n
\nNote: This variation of the url.format()
method is currently considered to\nbe experimental.
The url.parse()
method takes a URL string, parses it, and returns a URL\nobject.
The url.resolve()
method resolves a target URL relative to a base URL in a\nmanner similar to that of a Web browser resolving an anchor tag HREF.
For example:
\nurl.resolve('/one/two/three', 'four') // '/one/two/four'\nurl.resolve('http://example.com/', '/one') // 'http://example.com/one'\nurl.resolve('http://example.com/one', '/two') // 'http://example.com/two'\n
\n"
}
],
"type": "module",
"displayName": "URL"
}
]
}