url: support `path` for url.format
[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 the query string using the
73 `querystring` module. If `true` then the `query` property will always be
74 assigned an object, and the `search` property will always be a (possibly
75 empty) string.  Defaults to `false`.
76
77 Pass `true` as the third argument to treat `//foo/bar` as
78 `{ host: 'foo', pathname: '/bar' }` rather than
79 `{ pathname: '//foo/bar' }`. Defaults to `false`.
80
81 ## url.format(urlObj)
82
83 Take a parsed URL object, and return a formatted URL string.
84
85 * `href` will be ignored.
86 * `protocol` is treated the same with or without the trailing `:` (colon).
87   * The protocols `http`, `https`, `ftp`, `gopher`, `file` will be
88     postfixed with `://` (colon-slash-slash).
89   * All other protocols `mailto`, `xmpp`, `aim`, `sftp`, `foo`, etc will
90     be postfixed with `:` (colon)
91 * `slashes` set to `true` if the protocol requires `://` (colon-slash-slash)
92   * Only needs to be set for protocols not previously listed as requiring
93     slashes, such as `mongodb://localhost:8000/`
94 * `auth` will be used if present.
95 * `hostname` will only be used if `host` is absent.
96 * `port` will only be used if `host` is absent.
97 * `host` will be used in place of `hostname` and `port`
98 * `pathname` is treated the same with or without the leading `/` (slash).
99 * `path` is treated the same with `pathname` but able to contain `query` as well.
100 * `search` will be used in place of `query`.
101 * `query` (object; see `querystring`) will only be used if `search` is absent.
102 * `search` is treated the same with or without the leading `?` (question mark).
103 * `hash` is treated the same with or without the leading `#` (pound sign, anchor).
104
105 ## url.resolve(from, to)
106
107 Take a base URL, and a href URL, and resolve them as a browser would for
108 an anchor tag.  Examples:
109
110     url.resolve('/one/two/three', 'four')         // '/one/two/four'
111     url.resolve('http://example.com/', '/one')    // 'http://example.com/one'
112     url.resolve('http://example.com/one', '/two') // 'http://example.com/two'