5 This module has utilities for URL resolution and parsing.
6 Call `require('url')` to use it.
8 Parsed URL objects have some or all of the following fields, depending on
9 whether or not they exist in the URL string. Any parts that are not in the URL
10 string will not be in the parsed object. Examples are shown for the URL
12 `'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
14 * `href`: The full URL that was originally parsed. Both the protocol and host are lowercased.
16 Example: `'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
18 * `protocol`: The request protocol, lowercased.
22 * `slashes`: The protocol requires slashes after the colon
24 Example: true or false
26 * `host`: The full lowercased host portion of the URL, including port
29 Example: `'host.com:8080'`
31 * `auth`: The authentication information portion of a URL.
33 Example: `'user:pass'`
35 * `hostname`: Just the lowercased hostname portion of the host.
39 * `port`: The port number portion of the host.
43 * `pathname`: The path section of the URL, that comes after the host and
44 before the query, including the initial slash if present.
48 * `search`: The 'query string' portion of the URL, including the leading
51 Example: `'?query=string'`
53 * `path`: Concatenation of `pathname` and `search`.
55 Example: `'/p/a/t/h?query=string'`
57 * `query`: Either the 'params' portion of the query string, or a
58 querystring-parsed object.
60 Example: `'query=string'` or `{'query':'string'}`
62 * `hash`: The 'fragment' portion of the URL including the pound-sign.
66 The following methods are provided by the URL module:
68 ## url.parse(urlStr[, parseQueryString]\[, slashesDenoteHost])
70 Take a URL string, and return an object.
72 Pass `true` as the second argument to also parse
73 the query string using the `querystring` module.
76 Pass `true` as the third argument to treat `//foo/bar` as
77 `{ host: 'foo', pathname: '/bar' }` rather than
78 `{ pathname: '//foo/bar' }`. Defaults to `false`.
82 Take a parsed URL object, and return a formatted URL string.
84 * `href` will be ignored.
85 * `protocol` is treated the same with or without the trailing `:` (colon).
86 * The protocols `http`, `https`, `ftp`, `gopher`, `file` will be
87 postfixed with `://` (colon-slash-slash).
88 * All other protocols `mailto`, `xmpp`, `aim`, `sftp`, `foo`, etc will
89 be postfixed with `:` (colon)
90 * `slashes` set to `true` if the protocol requires `://` (colon-slash-slash)
91 * Only needs to be set for protocols not previously listed as requiring
92 slashes, such as `mongodb://localhost:8000/`
93 * `auth` will be used if present.
94 * `hostname` will only be used if `host` is absent.
95 * `port` will only be used if `host` is absent.
96 * `host` will be used in place of `hostname` and `port`
97 * `pathname` is treated the same with or without the leading `/` (slash)
98 * `search` will be used in place of `query`
99 * `query` (object; see `querystring`) will only be used if `search` is absent.
100 * `search` is treated the same with or without the leading `?` (question mark)
101 * `hash` is treated the same with or without the leading `#` (pound sign, anchor)
103 ## url.resolve(from, to)
105 Take a base URL, and a href URL, and resolve them as a browser would for
106 an anchor tag. Examples:
108 url.resolve('/one/two/three', 'four') // '/one/two/four'
109 url.resolve('http://example.com/', '/one') // 'http://example.com/one'
110 url.resolve('http://example.com/one', '/two') // 'http://example.com/two'