fc92bb7e8e8a69fb1c710b83ae2d36898e423d60
[platform/upstream/nodejs.git] / doc / api / url.markdown
1 # URL
2
3     Stability: 3 - Stable
4
5 This module has utilities for URL resolution and parsing.
6 Call `require('url')` to use it.
7
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
11
12 `'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
13
14 * `href`: The full URL that was originally parsed. Both the protocol and host are lowercased.
15
16     Example: `'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
17
18 * `protocol`: The request protocol, lowercased.
19
20     Example: `'http:'`
21
22 * `slashes`: The protocol requires slashes after the colon
23
24     Example: true or false
25
26 * `host`: The full lowercased host portion of the URL, including port
27   information.
28
29     Example: `'host.com:8080'`
30
31 * `auth`: The authentication information portion of a URL.
32
33     Example: `'user:pass'`
34
35 * `hostname`: Just the lowercased hostname portion of the host.
36
37     Example: `'host.com'`
38
39 * `port`: The port number portion of the host.
40
41     Example: `'8080'`
42
43 * `pathname`: The path section of the URL, that comes after the host and
44   before the query, including the initial slash if present.
45
46     Example: `'/p/a/t/h'`
47
48 * `search`: The 'query string' portion of the URL, including the leading
49   question mark.
50
51     Example: `'?query=string'`
52
53 * `path`: Concatenation of `pathname` and `search`.
54
55     Example: `'/p/a/t/h?query=string'`
56
57 * `query`: Either the 'params' portion of the query string, or a
58   querystring-parsed object.
59
60     Example: `'query=string'` or `{'query':'string'}`
61
62 * `hash`: The 'fragment' portion of the URL including the pound-sign.
63
64     Example: `'#hash'`
65
66 The following methods are provided by the URL module:
67
68 ## url.parse(urlStr[, parseQueryString]\[, slashesDenoteHost])
69
70 Take a URL string, and return an object.
71
72 Pass `true` as the second argument to also parse
73 the query string using the `querystring` module.
74 Defaults to `false`.
75
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`.
79
80 ## url.format(urlObj)
81
82 Take a parsed URL object, and return a formatted URL string.
83
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)
102
103 ## url.resolve(from, to)
104
105 Take a base URL, and a href URL, and resolve them as a browser would for
106 an anchor tag.  Examples:
107
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'