{"source":"doc/api/url.md","modules":[{"textRaw":"URL","name":"url","stability": 2,"stabilityText":"Stable","desc":"<p>The <code>url</code> module provides utilities for URL resolution and parsing. It can be\naccessed using:</p>\n<pre><code class=\"lang-js\">const url = require(&#39;url&#39;);\n</code></pre>\n","modules":[{"textRaw":"URL Strings and URL Objects","name":"url_strings_and_url_objects","desc":"<p>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.</p>\n<p>The following details each of the components of a parsed URL. The example\n<code>&#39;http://user:pass@host.com:8080/p/a/t/h?query=string#hash&#39;</code> is used to\nillustrate each.</p>\n<pre><code class=\"lang-txt\">┌─────────────────────────────────────────────────────────────────────────────┐\n│                                    href                                     │\n├──────────┬┬───────────┬─────────────────┬───────────────────────────┬───────┤\n│ protocol ││   auth    │      host       │           path            │ hash  │\n│          ││           ├──────────┬──────┼──────────┬────────────────┤       │\n│          ││           │ hostname │ port │ pathname │     search     │       │\n│          ││           │          │      │          ├─┬──────────────┤       │\n│          ││           │          │      │          │ │    query     │       │\n&quot;  http:   // user:pass @ host.com : 8080   /p/a/t/h  ?  query=string   #hash &quot;\n│          ││           │          │      │          │ │              │       │\n└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘\n(all spaces in the &quot;&quot; line should be ignored -- they&#39;re purely for formatting)\n</code></pre>\n","properties":[{"textRaw":"urlObject.href","name":"href","desc":"<p>The <code>href</code> property is the full URL string that was parsed with both the\n<code>protocol</code> and <code>host</code> components converted to lower-case.</p>\n<p>For example: <code>&#39;http://user:pass@host.com:8080/p/a/t/h?query=string#hash&#39;</code></p>\n"},{"textRaw":"urlObject.protocol","name":"protocol","desc":"<p>The <code>protocol</code> property identifies the URL&#39;s lower-cased protocol scheme.</p>\n<p>For example: <code>&#39;http:&#39;</code></p>\n"},{"textRaw":"urlObject.slashes","name":"slashes","desc":"<p>The <code>slashes</code> property is a <code>boolean</code> with a value of <code>true</code> if two ASCII\nforward-slash characters (<code>/</code>) are required following the colon in the\n<code>protocol</code>.</p>\n"},{"textRaw":"urlObject.host","name":"host","desc":"<p>The <code>host</code> property is the full lower-cased host portion of the URL, including\nthe <code>port</code> if specified.</p>\n<p>For example: <code>&#39;host.com:8080&#39;</code></p>\n"},{"textRaw":"urlObject.auth","name":"auth","desc":"<p>The <code>auth</code> property is the username and password portion of the URL, also\nreferred to as &quot;userinfo&quot;. This string subset follows the <code>protocol</code> and\ndouble slashes (if present) and preceeds the <code>host</code> component, delimited by an\nASCII &quot;at sign&quot; (<code>@</code>). The format of the string is <code>{username}[:{password}]</code>,\nwith the <code>[:{password}]</code> portion being optional.</p>\n<p>For example: <code>&#39;user:pass&#39;</code></p>\n"},{"textRaw":"urlObject.hostname","name":"hostname","desc":"<p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code>\ncomponent <em>without</em> the <code>port</code> included.</p>\n<p>For example: <code>&#39;host.com&#39;</code></p>\n"},{"textRaw":"urlObject.port","name":"port","desc":"<p>The <code>port</code> property is the numeric port portion of the <code>host</code> component.</p>\n<p>For example: <code>&#39;8080&#39;</code></p>\n"},{"textRaw":"urlObject.pathname","name":"pathname","desc":"<p>The <code>pathname</code> property consists of the entire path section of the URL. This\nis everything following the <code>host</code> (including the <code>port</code>) and before the start\nof the <code>query</code> or <code>hash</code> components, delimited by either the ASCII question\nmark (<code>?</code>) or hash (<code>#</code>) characters.</p>\n<p>For example <code>&#39;/p/a/t/h&#39;</code></p>\n<p>No decoding of the path string is performed.</p>\n"},{"textRaw":"urlObject.search","name":"search","desc":"<p>The <code>search</code> property consists of the entire &quot;query string&quot; portion of the\nURL, including the leading ASCII question mark (<code>?</code>) character.</p>\n<p>For example: <code>&#39;?query=string&#39;</code></p>\n<p>No decoding of the query string is performed.</p>\n"},{"textRaw":"urlObject.path","name":"path","desc":"<p>The <code>path</code> property is a concatenation of the <code>pathname</code> and <code>search</code>\ncomponents.</p>\n<p>For example: <code>&#39;/p/a/t/h?query=string&#39;</code></p>\n<p>No decoding of the <code>path</code> is performed.</p>\n"},{"textRaw":"urlObject.query","name":"query","desc":"<p>The <code>query</code> property is either the &quot;params&quot; portion of the query string (\neverything <em>except</em> the leading ASCII question mark (<code>?</code>), or an object\nreturned by the [<code>querystring</code>][]module&#39;s <code>parse()</code> method:</p>\n<p>For example: <code>&#39;query=string&#39;</code> or <code>{&#39;query&#39;: &#39;string&#39;}</code></p>\n<p>If returned as a string, no decoding of the query string is performed. If\nreturned as an object, both keys and values are decoded.</p>\n"},{"textRaw":"urlObject.hash","name":"hash","desc":"<p>The <code>hash</code> property consists of the &quot;fragment&quot; portion of the URL including\nthe leading ASCII hash (<code>#</code>) character.</p>\n<p>For example: <code>&#39;#hash&#39;</code></p>\n"}],"type":"module","displayName":"URL Strings and URL Objects"},{"textRaw":"Escaped Characters","name":"escaped_characters","desc":"<p>URLs are only permitted to contain a certain range of characters. Spaces (<code>&#39; &#39;</code>)\nand the following characters will be automatically escaped in the\nproperties of URL objects:</p>\n<pre><code class=\"lang-txt\">&lt; &gt; &quot; ` \\r \\n \\t{}| \\ ^ &#39;\n</code></pre>\n<p>For example, the ASCII space character (<code>&#39; &#39;</code>) is encoded as <code>%20</code>. The ASCII\nforward slash (<code>/</code>) character is encoded as <code>%3C</code>.</p>\n","type":"module","displayName":"Escaped Characters"}],"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":"<p>The <code>url.format()</code> method returns a formatted URL string derived from\n<code>urlObject</code>.</p>\n<p>If <code>urlObject</code> is not an object or a string, <code>url.parse()</code> will throw a\n[<code>TypeError</code>][].</p>\n<p>The formatting process operates as follows:</p>\n<ul>\n<li>A new empty string <code>result</code> is created.</li>\n<li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</li>\n<li>Otherwise, if <code>urlObject.protocol</code> is not <code>undefined</code> and is not a string, an\n[<code>Error</code>][]is thrown.</li>\n<li>For all string values of <code>urlObject.protocol</code> that <em>do not end</em> with an ASCII\ncolon (<code>:</code>) character, the literal string <code>:</code> will be appended to <code>result</code>.</li>\n<li>If either the <code>urlObject.slashes</code> property is true, <code>urlObject.protocol</code>\nbegins with one of <code>http</code>, <code>https</code>, <code>ftp</code>, <code>gopher</code>, or <code>file</code>, or\n<code>urlObject.protocol</code> is <code>undefined</code>, the literal string <code>//</code> will be appended\nto <code>result</code>.</li>\n<li>If the value of the <code>urlObject.auth</code> property is truthy, and either\n<code>urlObject.host</code> or <code>urlObject.hostname</code> are not <code>undefined</code>, the value of\n<code>urlObject.auth</code> will be coerced into a string and appended to <code>result</code>\n followed by the literal string <code>@</code>.</li>\n<li>If the <code>urlObject.host</code> property is <code>undefined</code> then:<ul>\n<li>If the <code>urlObject.hostname</code> is a string, it is appended to <code>result</code>.</li>\n<li>Otherwise, if <code>urlObject.hostname</code> is not <code>undefined</code> and is not a string,\nan [<code>Error</code>][]is thrown.</li>\n<li>If the <code>urlObject.port</code> property value is truthy, and <code>urlObject.hostname</code>\nis not <code>undefined</code>:<ul>\n<li>The literal string <code>:</code> is appended to <code>result</code>, and</li>\n<li>The value of <code>urlObject.port</code> is coerced to a string and appended to\n<code>result</code>.</li>\n</ul>\n</li>\n</ul>\n</li>\n<li>Otherwise, if the <code>urlObject.host</code> property value is truthy, the value of\n<code>urlObject.host</code> is coerced to a string and appended to <code>result</code>.</li>\n<li>If the <code>urlObject.pathname</code> property is a string that is not an empty string:<ul>\n<li>If the <code>urlObject.pathname</code> <em>does not start</em> with an ASCII forward slash\n(<code>/</code>), then the literal string &#39;/&#39; is appended to <code>result</code>.</li>\n<li>The value of <code>urlObject.pathname</code> is appended to <code>result</code>.</li>\n</ul>\n</li>\n<li>Otherwise, if <code>urlObject.pathname</code> is not <code>undefined</code> and is not a string, an\n[<code>Error</code>][]is thrown.</li>\n<li>If the <code>urlObject.search</code> property is <code>undefined</code> and if the <code>urlObject.query</code>\nproperty is an <code>Object</code>, the literal string <code>?</code> is appended to <code>result</code>\nfollowed by the output of calling the [<code>querystring</code>][]module&#39;s <code>stringify()</code>\nmethod passing the value of <code>urlObject.query</code>.</li>\n<li>Otherwise, if <code>urlObject.search</code> is a string:<ul>\n<li>If the value of <code>urlObject.search</code> <em>does not start</em> with the ASCII question\nmark (<code>?</code>) character, the literal string <code>?</code> is appended to <code>result</code>.</li>\n<li>The value of <code>urlObject.search</code> is appended to <code>result</code>.</li>\n</ul>\n</li>\n<li>Otherwise, if <code>urlObject.search</code> is not <code>undefined</code> and is not a string, an\n[<code>Error</code>][]is thrown.</li>\n<li>If the <code>urlObject.hash</code> property is a string:<ul>\n<li>If the value of <code>urlObject.hash</code> <em>does not start</em> with the ASCII hash (<code>#</code>)\ncharacter, the literal string <code>#</code> is appended to <code>result</code>.</li>\n<li>The value of <code>urlObject.hash</code> is appended to <code>result</code>.</li>\n</ul>\n</li>\n<li>Otherwise, if the <code>urlObject.hash</code> property is not <code>undefined</code> and is not a\nstring, an [<code>Error</code>][]is thrown.</li>\n<li><code>result</code> is returned.</li>\n</ul>\n"},{"textRaw":"url.parse(urlString[, parseQueryString[, slashesDenoteHost]])","type":"method","name":"parse","meta":{"added":["v0.1.25"]},"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 preceeding 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 preceeding 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":"<p>The <code>url.parse()</code> method takes a URL string, parses it, and returns a URL\nobject.</p>\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":"<p>The <code>url.resolve()</code> 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.</p>\n<p>For example:</p>\n<pre><code class=\"lang-js\">url.resolve(&#39;/one/two/three&#39;, &#39;four&#39;)         // &#39;/one/two/four&#39;\nurl.resolve(&#39;http://example.com/&#39;, &#39;/one&#39;)    // &#39;http://example.com/one&#39;\nurl.resolve(&#39;http://example.com/one&#39;, &#39;/two&#39;) // &#39;http://example.com/two&#39;\n</code></pre>\n"}],"type":"module","displayName":"URL"}]}