Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 100 lines (63 sloc) 3.152 kB
227307e @tj Added node.*.md files
authored
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 * `host`: The full lowercased host portion of the URL, including port
23 information.
24
25 Example: `'host.com:8080'`
26
27 * `auth`: The authentication information portion of a URL.
28
29 Example: `'user:pass'`
30
31 * `hostname`: Just the lowercased hostname portion of the host.
32
33 Example: `'host.com'`
34
35 * `port`: The port number portion of the host.
36
37 Example: `'8080'`
38
39 * `pathname`: The path section of the URL, that comes after the host and
40 before the query, including the initial slash if present.
41
42 Example: `'/p/a/t/h'`
43
44 * `search`: The 'query string' portion of the URL, including the leading
45 question mark.
46
47 Example: `'?query=string'`
48
49 * `path`: Concatenation of `pathname` and `search`.
50
51 Example: `'/p/a/t/h?query=string'`
52
53 * `query`: Either the 'params' portion of the query string, or a
54 querystring-parsed object.
55
56 Example: `'query=string'` or `{'query':'string'}`
57
58 * `hash`: The 'fragment' portion of the URL including the pound-sign.
59
60 Example: `'#hash'`
61
62 The following methods are provided by the URL module:
63
64 ## url.parse(urlStr, [parseQueryString], [slashesDenoteHost])
65
66 Take a URL string, and return an object.
67
68 Pass `true` as the second argument to also parse
69 the query string using the `querystring` module.
70 Defaults to `false`.
71
72 Pass `true` as the third argument to treat `//foo/bar` as
73 `{ host: 'foo', pathname: '/bar' }` rather than
74 `{ pathname: '//foo/bar' }`. Defaults to `false`.
75
76 ## url.format(urlObj)
77
78 Take a parsed URL object, and return a formatted URL string.
79
80 * `href` will be ignored.
81 * `protocol`is treated the same with or without the trailing `:` (colon).
82 * The protocols `http`, `https`, `ftp`, `gopher`, `file` will be
83 postfixed with `://` (colon-slash-slash).
84 * All other protocols `mailto`, `xmpp`, `aim`, `sftp`, `foo`, etc will
85 be postfixed with `:` (colon)
86 * `auth` will be used if present.
87 * `hostname` will only be used if `host` is absent.
88 * `port` will only be used if `host` is absent.
89 * `host` will be used in place of `hostname` and `port`
90 * `pathname` is treated the same with or without the leading `/` (slash)
91 * `search` will be used in place of `query`
92 * `query` (object; see `querystring`) will only be used if `search` is absent.
93 * `search` is treated the same with or without the leading `?` (question mark)
94 * `hash` is treated the same with or without the leading `#` (pound sign, anchor)
95
96 ## url.resolve(from, to)
97
98 Take a base URL, and a href URL, and resolve them as a browser would for
99 an anchor tag.
Something went wrong with that request. Please try again.