1
|
# node-url
|
2
|
|
3
|
[![Build Status](https://travis-ci.org/defunctzombie/node-url.svg?branch=master)](https://travis-ci.org/defunctzombie/node-url)
|
4
|
|
5
|
This module has utilities for URL resolution and parsing meant to have feature parity with node.js core [url](http://nodejs.org/api/url.html) module.
|
6
|
|
7
|
```js
|
8
|
var url = require('url');
|
9
|
```
|
10
|
|
11
|
## api
|
12
|
|
13
|
Parsed URL objects have some or all of the following fields, depending on
|
14
|
whether or not they exist in the URL string. Any parts that are not in the URL
|
15
|
string will not be in the parsed object. Examples are shown for the URL
|
16
|
|
17
|
`'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
|
18
|
|
19
|
* `href`: The full URL that was originally parsed. Both the protocol and host are lowercased.
|
20
|
|
21
|
Example: `'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
|
22
|
|
23
|
* `protocol`: The request protocol, lowercased.
|
24
|
|
25
|
Example: `'http:'`
|
26
|
|
27
|
* `host`: The full lowercased host portion of the URL, including port
|
28
|
information.
|
29
|
|
30
|
Example: `'host.com:8080'`
|
31
|
|
32
|
* `auth`: The authentication information portion of a URL.
|
33
|
|
34
|
Example: `'user:pass'`
|
35
|
|
36
|
* `hostname`: Just the lowercased hostname portion of the host.
|
37
|
|
38
|
Example: `'host.com'`
|
39
|
|
40
|
* `port`: The port number portion of the host.
|
41
|
|
42
|
Example: `'8080'`
|
43
|
|
44
|
* `pathname`: The path section of the URL, that comes after the host and
|
45
|
before the query, including the initial slash if present.
|
46
|
|
47
|
Example: `'/p/a/t/h'`
|
48
|
|
49
|
* `search`: The 'query string' portion of the URL, including the leading
|
50
|
question mark.
|
51
|
|
52
|
Example: `'?query=string'`
|
53
|
|
54
|
* `path`: Concatenation of `pathname` and `search`.
|
55
|
|
56
|
Example: `'/p/a/t/h?query=string'`
|
57
|
|
58
|
* `query`: Either the 'params' portion of the query string, or a
|
59
|
querystring-parsed object.
|
60
|
|
61
|
Example: `'query=string'` or `{'query':'string'}`
|
62
|
|
63
|
* `hash`: The 'fragment' portion of the URL including the pound-sign.
|
64
|
|
65
|
Example: `'#hash'`
|
66
|
|
67
|
The following methods are provided by the URL module:
|
68
|
|
69
|
### url.parse(urlStr, [parseQueryString], [slashesDenoteHost])
|
70
|
|
71
|
Take a URL string, and return an object.
|
72
|
|
73
|
Pass `true` as the second argument to also parse
|
74
|
the query string using the `querystring` module.
|
75
|
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
|
* `auth` will be used if present.
|
92
|
* `hostname` will only be used if `host` is absent.
|
93
|
* `port` will only be used if `host` is absent.
|
94
|
* `host` will be used in place of `hostname` and `port`
|
95
|
* `pathname` is treated the same with or without the leading `/` (slash)
|
96
|
* `search` will be used in place of `query`
|
97
|
* `query` (object; see `querystring`) will only be used if `search` is absent.
|
98
|
* `search` is treated the same with or without the leading `?` (question mark)
|
99
|
* `hash` is treated the same with or without the leading `#` (pound sign, anchor)
|
100
|
|
101
|
### url.resolve(from, to)
|
102
|
|
103
|
Take a base URL, and a href URL, and resolve them as a browser would for
|
104
|
an anchor tag. Examples:
|
105
|
|
106
|
url.resolve('/one/two/three', 'four') // '/one/two/four'
|
107
|
url.resolve('http://example.com/', '/one') // 'http://example.com/one'
|
108
|
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
|