{ "source": "doc/api/url.md", "modules": [ { "textRaw": "URL", "name": "url", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "

The url module provides utilities for URL resolution and parsing. It can be\naccessed using:

\n
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.

\n

The url module provides two APIs for working with URLs: a legacy API that is\nNode.js specific, and a newer API that implements the same\nWHATWG URL Standard used by web browsers.

\n

Note: While the Legacy API has not been deprecated, it is maintained solely\nfor backwards compatibility with existing applications. New application code\nshould use the WHATWG API.

\n

A comparison between the WHATWG and Legacy APIs is provided below. Above the URL\n'http://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash', properties of\nan object returned by the legacy url.parse() are shown. Below it are\nproperties of a WHATWG URL object.

\n

Note: WHATWG URL's origin property includes protocol and host, but not\nusername or password.

\n
┌─────────────────────────────────────────────────────────────────────────────────────────────┐\n│                                            href                                             │\n├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤\n│ protocol │  │        auth         │        host         │           path            │ hash  │\n│          │  │                     ├──────────────┬──────┼──────────┬────────────────┤       │\n│          │  │                     │   hostname   │ port │ pathname │     search     │       │\n│          │  │                     │              │      │          ├─┬──────────────┤       │\n│          │  │                     │              │      │          │ │    query     │       │\n"  https:   //    user   :   pass   @ sub.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

Parsing the URL string using the WHATWG API:

\n
const { URL } = require('url');\nconst myURL =\n  new URL('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');\n
\n

Note: In Web Browsers, the WHATWG URL class is a global that is always\navailable. In Node.js, however, the URL class must be accessed via\nrequire('url').URL.

\n

Parsing the URL string using the Legacy API:

\n
const url = require('url');\nconst myURL =\n  url.parse('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');\n
\n", "type": "module", "displayName": "URL Strings and URL Objects" }, { "textRaw": "The WHATWG URL API", "name": "the_whatwg_url_api", "meta": { "added": [ "v7.0.0, v6.13.0" ] }, "classes": [ { "textRaw": "Class: URL", "type": "class", "name": "URL", "desc": "

Browser-compatible URL class, implemented by following the WHATWG URL\nStandard. Examples of parsed URLs may be found in the Standard itself.

\n

Note: In accordance with browser conventions, all properties of URL objects\nare implemented as getters and setters on the class prototype, rather than as\ndata properties on the object itself. Thus, unlike legacy urlObjects, using\nthe delete keyword on any properties of URL objects (e.g. delete\nmyURL.protocol, delete myURL.pathname, etc) has no effect but will still\nreturn true.

\n", "modules": [ { "textRaw": "Constructor: new URL(input[, base])", "name": "constructor:_new_url(input[,_base])", "desc": "\n

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).

\n
const { URL } = require('url');\nconst myURL = new URL('/foo', 'https://example.org/');\n// https://example.org/foo\n
\n

A 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:

\n
const { URL } = require('url');\nconst myURL = new URL({ toString: () => 'https://example.org/' });\n// https://example.org/\n
\n

Unicode characters appearing within the hostname of input will be\nautomatically converted to ASCII using the Punycode algorithm.

\n
const { URL } = require('url');\nconst myURL = new URL('https://你好你好');\n// https://xn--6qqa088eba/\n
\n

Note: This feature is only available if the node executable was compiled\nwith ICU enabled. If not, the domain names are passed through unchanged.

\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.

\n
const { URL } = require('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
\n

Invalid 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.

\n" }, { "textRaw": "`host` {string} ", "type": "string", "name": "host", "desc": "

Gets and sets the host portion of the URL.

\n
const { URL } = require('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
\n

Invalid host values assigned to the host property are ignored.

\n" }, { "textRaw": "`hostname` {string} ", "type": "string", "name": "hostname", "desc": "

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.

\n
const { URL } = require('url');\nconst 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
\n

Invalid hostname values assigned to the hostname property are ignored.

\n" }, { "textRaw": "`href` {string} ", "type": "string", "name": "href", "desc": "

Gets and sets the serialized URL.

\n
const { URL } = require('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';\nconsole.log(myURL.href);\n// Prints https://example.com/bar\n
\n

Getting the value of the href property is equivalent to calling\nurl.toString().

\n

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.

\n

If the value assigned to the href property is not a valid URL, a TypeError\nwill be thrown.

\n" }, { "textRaw": "`origin` {string} ", "type": "string", "name": "origin", "desc": "

Gets the read-only serialization of the URL's origin.

\n
const { URL } = require('url');\nconst myURL = new URL('https://example.org/foo/bar?baz');\nconsole.log(myURL.origin);\n// Prints https://example.org\n
\n
const { URL } = require('url');\nconst idnURL = new URL('https://你好你好');\nconsole.log(idnURL.origin);\n// Prints https://xn--6qqa088eba\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.

\n
const { URL } = require('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
\n

Invalid 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.

\n" }, { "textRaw": "`pathname` {string} ", "type": "string", "name": "pathname", "desc": "

Gets and sets the path portion of the URL.

\n
const { URL } = require('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
\n

Invalid 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.

\n" }, { "textRaw": "`port` {string} ", "type": "string", "name": "port", "desc": "

Gets and sets the port portion of the URL.

\n
const { URL } = require('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
\n

The 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 ('').

\n

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.

\n" }, { "textRaw": "`protocol` {string} ", "type": "string", "name": "protocol", "desc": "

Gets and sets the protocol portion of the URL.

\n
const { URL } = require('url');\nconst myURL = new URL('https://example.org');\nconsole.log(myURL.protocol);\n// Prints https:\n\nmyURL.protocol = 'ftp';\nconsole.log(myURL.href);\n// Prints ftp://example.org/\n
\n

Invalid URL protocol values assigned to the protocol property are ignored.

\n" }, { "textRaw": "`search` {string} ", "type": "string", "name": "search", "desc": "

Gets and sets the serialized query portion of the URL.

\n
const { URL } = require('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
\n

Any 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.

\n" }, { "textRaw": "`searchParams` {URLSearchParams} ", "type": "URLSearchParams", "name": "searchParams", "desc": "

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.

\n" }, { "textRaw": "`username` {string} ", "type": "string", "name": "username", "desc": "

Gets and sets the username portion of the URL.

\n
const { URL } = require('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
\n

Any 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.

\n" } ], "methods": [ { "textRaw": "url.toString()", "type": "method", "name": "toString", "signatures": [ { "return": { "textRaw": "Returns: {string} ", "name": "return", "type": "string" }, "params": [] }, { "params": [] } ], "desc": "

The toString() method on the URL object returns the serialized URL. The\nvalue returned is equivalent to that of url.href and url.toJSON().

\n

Because of the need for standard compliance, this method does not allow users\nto customize the serialization process of the URL.

\n" }, { "textRaw": "url.toJSON()", "type": "method", "name": "toJSON", "signatures": [ { "return": { "textRaw": "Returns: {string} ", "name": "return", "type": "string" }, "params": [] }, { "params": [] } ], "desc": "

The toJSON() method on the URL object returns the serialized URL. The\nvalue returned is equivalent to that of url.href and\nurl.toString().

\n

This method is automatically called when an URL object is serialized\nwith JSON.stringify().

\n
const { URL } = require('url');\nconst 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", "meta": { "added": [ "v7.5.0, v6.13.0" ] }, "desc": "

The URLSearchParams API provides read and write access to the query of a\nURL. The URLSearchParams class can also be used standalone with one of the\nfour following constructors.

\n

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.

\n
const { URL, URLSearchParams } = require('url');\n\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\nconst newSearchParams = new URLSearchParams(myURL.searchParams);\n// The above is equivalent to\n// const newSearchParams = new URLSearchParams(myURL.search);\n\nnewSearchParams.append('a', 'c');\nconsole.log(myURL.href);\n// Prints https://example.org/?a=b\nconsole.log(newSearchParams.toString());\n// Prints a=b&a=c\n\n// newSearchParams.toString() is implicitly called\nmyURL.search = newSearchParams;\nconsole.log(myURL.href);\n// Prints https://example.org/?a=b&a=c\nnewSearchParams.delete('a');\nconsole.log(myURL.href);\n// Prints https://example.org/?a=b&a=c\n
\n", "modules": [ { "textRaw": "Constructor: new URLSearchParams()", "name": "constructor:_new_urlsearchparams()", "desc": "

Instantiate a new empty URLSearchParams object.

\n", "type": "module", "displayName": "Constructor: new URLSearchParams()" }, { "textRaw": "Constructor: new URLSearchParams(string)", "name": "constructor:_new_urlsearchparams(string)", "desc": "\n

Parse the string as a query string, and use it to instantiate a new\nURLSearchParams object. A leading '?', if present, is ignored.

\n
const { URLSearchParams } = require('url');\nlet params;\n\nparams = new URLSearchParams('user=abc&query=xyz');\nconsole.log(params.get('user'));\n// Prints 'abc'\nconsole.log(params.toString());\n// Prints 'user=abc&query=xyz'\n\nparams = new URLSearchParams('?user=abc&query=xyz');\nconsole.log(params.toString());\n// Prints 'user=abc&query=xyz'\n
\n", "type": "module", "displayName": "Constructor: new URLSearchParams(string)" }, { "textRaw": "Constructor: new URLSearchParams(obj)", "name": "constructor:_new_urlsearchparams(obj)", "meta": { "added": [ "v7.10.0, v6.13.0" ] }, "desc": "\n

Instantiate a new URLSearchParams object with a query hash map. The key and\nvalue of each property of obj are always coerced to strings.

\n

Note: Unlike querystring module, duplicate keys in the form of array\nvalues are not allowed. Arrays are stringified using array.toString(),\nwhich simply joins all array elements with commas.

\n
const { URLSearchParams } = require('url');\nconst params = new URLSearchParams({\n  user: 'abc',\n  query: ['first', 'second']\n});\nconsole.log(params.getAll('query'));\n// Prints [ 'first,second' ]\nconsole.log(params.toString());\n// Prints 'user=abc&query=first%2Csecond'\n
\n", "type": "module", "displayName": "Constructor: new URLSearchParams(obj)" }, { "textRaw": "Constructor: new URLSearchParams(iterable)", "name": "constructor:_new_urlsearchparams(iterable)", "meta": { "added": [ "v7.10.0, v6.13.0" ] }, "desc": "\n

Instantiate a new URLSearchParams object with an iterable map in a way that\nis similar to Map's constructor. iterable can be an Array or any\niterable object. That means iterable can be another URLSearchParams, in\nwhich case the constructor will simply create a clone of the provided\nURLSearchParams. Elements of iterable are key-value pairs, and can\nthemselves be any iterable object.

\n

Duplicate keys are allowed.

\n
const { URLSearchParams } = require('url');\nlet params;\n\n// Using an array\nparams = new URLSearchParams([\n  ['user', 'abc'],\n  ['query', 'first'],\n  ['query', 'second']\n]);\nconsole.log(params.toString());\n// Prints 'user=abc&query=first&query=second'\n\n// Using a Map object\nconst map = new Map();\nmap.set('user', 'abc');\nmap.set('query', 'xyz');\nparams = new URLSearchParams(map);\nconsole.log(params.toString());\n// Prints 'user=abc&query=xyz'\n\n// Using a generator function\nfunction* getQueryPairs() {\n  yield ['user', 'abc'];\n  yield ['query', 'first'];\n  yield ['query', 'second'];\n}\nparams = new URLSearchParams(getQueryPairs());\nconsole.log(params.toString());\n// Prints 'user=abc&query=first&query=second'\n\n// Each key-value pair must have exactly two elements\nnew URLSearchParams([\n  ['user', 'abc', 'error']\n]);\n// Throws TypeError [ERR_INVALID_TUPLE]:\n//        Each query pair must be an iterable [name, value] tuple\n
\n", "type": "module", "displayName": "Constructor: new URLSearchParams(iterable)" } ], "methods": [ { "textRaw": "urlSearchParams.append(name, value)", "type": "method", "name": "append", "signatures": [ { "params": [ { "textRaw": "`name` {string} ", "name": "name", "type": "string" }, { "textRaw": "`value` {string} ", "name": "value", "type": "string" } ] }, { "params": [ { "name": "name" }, { "name": "value" } ] } ], "desc": "

Append 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.

\n" }, { "textRaw": "urlSearchParams.entries()", "type": "method", "name": "entries", "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.\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.

\n

Alias for urlSearchParams[@@iterator]().

\n" }, { "textRaw": "urlSearchParams.forEach(fn[, thisArg])", "type": "method", "name": "forEach", "signatures": [ { "params": [ { "textRaw": "`fn` {Function} Function invoked for each name-value pair in the query. ", "name": "fn", "type": "Function", "desc": "Function invoked for each name-value pair in the query." }, { "textRaw": "`thisArg` {Object} Object to be used as `this` value for when `fn` is called ", "name": "thisArg", "type": "Object", "desc": "Object to be used as `this` value for when `fn` is called", "optional": true } ] }, { "params": [ { "name": "fn" }, { "name": "thisArg", "optional": true } ] } ], "desc": "

Iterates over each name-value pair in the query and invokes the given function.

\n
const { URL } = require('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} or `null` if there is no name-value pair with the given `name`. ", "name": "return", "type": "string", "desc": "or `null` if there is no name-value pair with the given `name`." }, "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.

\n" }, { "textRaw": "urlSearchParams.getAll(name)", "type": "method", "name": "getAll", "signatures": [ { "return": { "textRaw": "Returns: {Array} ", "name": "return", "type": "Array" }, "params": [ { "textRaw": "`name` {string} ", "name": "name", "type": "string" } ] }, { "params": [ { "name": "name" } ] } ], "desc": "

Returns the values of all name-value pairs whose name is name. If there are\nno such pairs, an empty array is returned.

\n" }, { "textRaw": "urlSearchParams.has(name)", "type": "method", "name": "has", "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`name` {string} ", "name": "name", "type": "string" } ] }, { "params": [ { "name": "name" } ] } ], "desc": "

Returns true if there is at least one name-value pair whose name is name.

\n" }, { "textRaw": "urlSearchParams.keys()", "type": "method", "name": "keys", "signatures": [ { "return": { "textRaw": "Returns: {Iterator} ", "name": "return", "type": "Iterator" }, "params": [] }, { "params": [] } ], "desc": "

Returns an ES6 Iterator over the names of each name-value pair.

\n
const { 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.

\n
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", "meta": { "added": [ "v7.7.0, v6.13.0" ] }, "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.

\n

This method can be used, in particular, to increase cache hits.

\n
const { URLSearchParams } = require('url');\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.

\n

Alias for urlSearchParams.entries().

\n
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": "url.domainToASCII(domain)", "type": "method", "name": "domainToASCII", "meta": { "added": [ "v7.4.0, v6.13.0" ] }, "signatures": [ { "return": { "textRaw": "Returns: {string} ", "name": "return", "type": "string" }, "params": [ { "textRaw": "`domain` {string} ", "name": "domain", "type": "string" } ] }, { "params": [ { "name": "domain" } ] } ], "desc": "

Returns the Punycode ASCII serialization of the domain. If domain is an\ninvalid domain, the empty string is returned.

\n

It performs the inverse operation to url.domainToUnicode().

\n
const url = require('url');\nconsole.log(url.domainToASCII('español.com'));\n// Prints xn--espaol-zwa.com\nconsole.log(url.domainToASCII('中文.com'));\n// Prints xn--fiq228c.com\nconsole.log(url.domainToASCII('xn--iñvalid.com'));\n// Prints an empty string\n
\n" }, { "textRaw": "url.domainToUnicode(domain)", "type": "method", "name": "domainToUnicode", "meta": { "added": [ "v7.4.0, v6.13.0" ] }, "signatures": [ { "return": { "textRaw": "Returns: {string} ", "name": "return", "type": "string" }, "params": [ { "textRaw": "`domain` {string} ", "name": "domain", "type": "string" } ] }, { "params": [ { "name": "domain" } ] } ], "desc": "

Returns the Unicode serialization of the domain. If domain is an invalid\ndomain, the empty string is returned.

\n

It performs the inverse operation to url.domainToASCII().

\n
const url = require('url');\nconsole.log(url.domainToUnicode('xn--espaol-zwa.com'));\n// Prints español.com\nconsole.log(url.domainToUnicode('xn--fiq228c.com'));\n// Prints 中文.com\nconsole.log(url.domainToUnicode('xn--iñvalid.com'));\n// Prints an empty string\n
\n" } ], "type": "module", "displayName": "The WHATWG URL API" }, { "textRaw": "Legacy URL API", "name": "legacy_url_api", "modules": [ { "textRaw": "Legacy urlObject", "name": "legacy_urlobject", "desc": "

The legacy urlObject (require('url').Url) is created and returned by the\nurl.parse() function.

\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.

\n

For example: 'user:pass'

\n" }, { "textRaw": "urlObject.hash", "name": "hash", "desc": "

The hash property consists of the "fragment" portion of the URL including\nthe leading ASCII hash (#) character.

\n

For example: '#hash'

\n" }, { "textRaw": "urlObject.host", "name": "host", "desc": "

The host property is the full lower-cased host portion of the URL, including\nthe port if specified.

\n

For example: 'sub.host.com:8080'

\n" }, { "textRaw": "urlObject.hostname", "name": "hostname", "desc": "

The hostname property is the lower-cased host name portion of the host\ncomponent without the port included.

\n

For example: 'sub.host.com'

\n" }, { "textRaw": "urlObject.href", "name": "href", "desc": "

The href property is the full URL string that was parsed with both the\nprotocol and host components converted to lower-case.

\n

For example: 'http://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash'

\n" } ], "type": "module", "displayName": "Legacy urlObject" } ], "properties": [ { "textRaw": "urlObject.port", "name": "port", "desc": "

The port property is the numeric port portion of the host component.

\n

For example: '8080'

\n" }, { "textRaw": "urlObject.pathname", "name": "pathname", "desc": "

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.

\n

For example '/p/a/t/h'

\n

No decoding of the path string is performed.

\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.

\n

For example: '?query=string'

\n

No decoding of the query string is performed.

\n" }, { "textRaw": "urlObject.path", "name": "path", "desc": "

The path property is a concatenation of the pathname and search\ncomponents.

\n

For example: '/p/a/t/h?query=string'

\n

No decoding of the path is performed.

\n" }, { "textRaw": "urlObject.query", "name": "query", "desc": "

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().

\n

For example: 'query=string' or {'query': 'string'}

\n

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", "properties": [ { "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.

\n

For example: '?query=string'

\n

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.

\n" } ] } ], "methods": [ { "textRaw": "url.format(urlObject)", "type": "method", "name": "format", "meta": { "added": [ "v0.1.25" ] }, "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.

\n

If urlObject is not an object or a string, url.format() will throw a\nTypeError.

\n

The formatting process operates as follows:

\n\n" }, { "textRaw": "url.parse(urlString[, parseQueryString[, slashesDenoteHost]])", "type": "method", "name": "parse", "meta": { "added": [ "v0.1.25" ], "changes": [ { "version": "v9.0.0", "pr-url": "https://github.com/nodejs/node/pull/13606", "description": "The `search` property on the returned URL object is now `null` when no query string is present." } ] }, "signatures": [ { "params": [ { "textRaw": "`urlString` {string} The URL string to parse. ", "name": "urlString", "type": "string", "desc": "The URL string to parse." }, { "textRaw": "`parseQueryString` {boolean} If `true`, the `query` property will always be set to an object returned by the [`querystring`][] module's `parse()` method. If `false`, the `query` property on the returned URL object will be an unparsed, undecoded string. Defaults to `false`. ", "name": "parseQueryString", "type": "boolean", "desc": "If `true`, the `query` property will always be set to an object returned by the [`querystring`][] module's `parse()` method. If `false`, the `query` property on the returned URL object will be an unparsed, undecoded string. Defaults to `false`.", "optional": true }, { "textRaw": "`slashesDenoteHost` {boolean} If `true`, the first token after the literal string `//` and preceding the next `/` will be interpreted as the `host`. For instance, given `//foo/bar`, the result would be `{host: 'foo', pathname: '/bar'}` rather than `{pathname: '//foo/bar'}`. Defaults to `false`. ", "name": "slashesDenoteHost", "type": "boolean", "desc": "If `true`, the first token after the literal string `//` and preceding the next `/` will be interpreted as the `host`. For instance, given `//foo/bar`, the result would be `{host: 'foo', pathname: '/bar'}` rather than `{pathname: '//foo/bar'}`. Defaults to `false`.", "optional": true } ] }, { "params": [ { "name": "urlString" }, { "name": "parseQueryString", "optional": true }, { "name": "slashesDenoteHost", "optional": true } ] } ], "desc": "

The url.parse() method takes a URL string, parses it, and returns a URL\nobject.

\n" }, { "textRaw": "url.resolve(from, to)", "type": "method", "name": "resolve", "meta": { "added": [ "v0.1.25" ] }, "signatures": [ { "params": [ { "textRaw": "`from` {string} The Base URL being resolved against. ", "name": "from", "type": "string", "desc": "The Base URL being resolved against." }, { "textRaw": "`to` {string} The HREF URL being resolved. ", "name": "to", "type": "string", "desc": "The HREF URL being resolved." } ] }, { "params": [ { "name": "from" }, { "name": "to" } ] } ], "desc": "

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.

\n

For example:

\n
url.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

\n" } ], "type": "module", "displayName": "Legacy URL API" }, { "textRaw": "Percent-Encoding in URLs", "name": "percent-encoding_in_urls", "desc": "

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.

\n", "modules": [ { "textRaw": "Legacy API", "name": "legacy_api", "desc": "

Within the Legacy API, spaces (' ') and the following characters will be\nautomatically escaped in the properties of URL objects:

\n
< > " ` \\r \\n \\t { } | \\ ^ '\n
\n

For example, the ASCII space character (' ') is encoded as %20. The ASCII\nforward slash (/) character is encoded as %3C.

\n", "type": "module", "displayName": "Legacy API" }, { "textRaw": "WHATWG API", "name": "whatwg_api", "desc": "

The WHATWG URL Standard uses a more selective and fine grained approach to\nselecting encoded characters than that used by the Legacy API.

\n

The WHATWG algorithm defines three "percent-encode sets" that describe ranges\nof characters that must be percent-encoded:

\n\n

The userinfo percent-encode set is used exclusively for username and\npasswords encoded within the URL. The path percent-encode set is used for the\npath of most URLs. The C0 control percent-encode set is used for all\nother cases, including URL fragments in particular, but also host and path\nunder certain specific conditions.

\n

When 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:

\n
const { URL } = require('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": "WHATWG API" } ], "type": "module", "displayName": "Percent-Encoding in URLs" } ], "type": "module", "displayName": "URL" } ] }