2012-02-27 20:09:35 +01:00
|
|
|
# URL
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-03 00:14:03 +01:00
|
|
|
Stability: 3 - Stable
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
This module has utilities for URL resolution and parsing.
|
|
|
|
Call `require('url')` to use it.
|
|
|
|
|
|
|
|
Parsed URL objects have some or all of the following fields, depending on
|
|
|
|
whether or not they exist in the URL string. Any parts that are not in the URL
|
|
|
|
string will not be in the parsed object. Examples are shown for the URL
|
|
|
|
|
|
|
|
`'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
|
|
|
|
|
2011-04-10 07:56:26 +02:00
|
|
|
* `href`: The full URL that was originally parsed. Both the protocol and host are lowercased.
|
2010-11-21 23:22:34 +01:00
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Example: `'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'`
|
|
|
|
|
2011-04-10 07:56:26 +02:00
|
|
|
* `protocol`: The request protocol, lowercased.
|
2010-11-21 23:22:34 +01:00
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Example: `'http:'`
|
|
|
|
|
2014-05-07 12:59:23 +02:00
|
|
|
* `slashes`: The protocol requires slashes after the colon
|
|
|
|
|
|
|
|
Example: true or false
|
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
* `host`: The full lowercased host portion of the URL, including port
|
|
|
|
information.
|
|
|
|
|
|
|
|
Example: `'host.com:8080'`
|
2010-11-21 23:22:34 +01:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
* `auth`: The authentication information portion of a URL.
|
2010-11-21 23:22:34 +01:00
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Example: `'user:pass'`
|
|
|
|
|
2011-04-10 07:56:26 +02:00
|
|
|
* `hostname`: Just the lowercased hostname portion of the host.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Example: `'host.com'`
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
* `port`: The port number portion of the host.
|
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Example: `'8080'`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
* `pathname`: The path section of the URL, that comes after the host and
|
|
|
|
before the query, including the initial slash if present.
|
|
|
|
|
|
|
|
Example: `'/p/a/t/h'`
|
|
|
|
|
|
|
|
* `search`: The 'query string' portion of the URL, including the leading
|
|
|
|
question mark.
|
|
|
|
|
|
|
|
Example: `'?query=string'`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-10-20 00:06:49 +02:00
|
|
|
* `path`: Concatenation of `pathname` and `search`.
|
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Example: `'/p/a/t/h?query=string'`
|
|
|
|
|
|
|
|
* `query`: Either the 'params' portion of the query string, or a
|
|
|
|
querystring-parsed object.
|
|
|
|
|
|
|
|
Example: `'query=string'` or `{'query':'string'}`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
* `hash`: The 'fragment' portion of the URL including the pound-sign.
|
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Example: `'#hash'`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
The following methods are provided by the URL module:
|
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## url.parse(urlStr[, parseQueryString]\[, slashesDenoteHost])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-08-17 22:13:56 +02:00
|
|
|
Take a URL string, and return an object.
|
|
|
|
|
|
|
|
Pass `true` as the second argument to also parse
|
2010-10-28 14:18:16 +02:00
|
|
|
the query string using the `querystring` module.
|
2011-12-27 09:43:58 +01:00
|
|
|
Defaults to `false`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-08-17 22:13:56 +02:00
|
|
|
Pass `true` as the third argument to treat `//foo/bar` as
|
|
|
|
`{ host: 'foo', pathname: '/bar' }` rather than
|
2011-12-27 09:43:58 +01:00
|
|
|
`{ pathname: '//foo/bar' }`. Defaults to `false`.
|
2011-08-17 22:13:56 +02:00
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
## url.format(urlObj)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Take a parsed URL object, and return a formatted URL string.
|
|
|
|
|
2011-09-01 20:23:17 +02:00
|
|
|
* `href` will be ignored.
|
2014-04-07 04:08:50 +02:00
|
|
|
* `protocol` is treated the same with or without the trailing `:` (colon).
|
2012-04-30 19:30:05 +02:00
|
|
|
* The protocols `http`, `https`, `ftp`, `gopher`, `file` will be
|
|
|
|
postfixed with `://` (colon-slash-slash).
|
|
|
|
* All other protocols `mailto`, `xmpp`, `aim`, `sftp`, `foo`, etc will
|
|
|
|
be postfixed with `:` (colon)
|
2014-05-07 12:59:23 +02:00
|
|
|
* `slashes` set to `true` if the protocol requires `://` (colon-slash-slash)
|
|
|
|
* Only needs to be set for protocols not previously listed as requiring
|
|
|
|
slashes, such as `mongodb://localhost:8000/`
|
2012-04-30 19:30:05 +02:00
|
|
|
* `auth` will be used if present.
|
2011-09-01 20:23:17 +02:00
|
|
|
* `hostname` will only be used if `host` is absent.
|
|
|
|
* `port` will only be used if `host` is absent.
|
2012-04-08 18:07:28 +02:00
|
|
|
* `host` will be used in place of `hostname` and `port`
|
2011-09-01 20:23:17 +02:00
|
|
|
* `pathname` is treated the same with or without the leading `/` (slash)
|
|
|
|
* `search` will be used in place of `query`
|
|
|
|
* `query` (object; see `querystring`) will only be used if `search` is absent.
|
|
|
|
* `search` is treated the same with or without the leading `?` (question mark)
|
|
|
|
* `hash` is treated the same with or without the leading `#` (pound sign, anchor)
|
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
## url.resolve(from, to)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-04-30 19:30:05 +02:00
|
|
|
Take a base URL, and a href URL, and resolve them as a browser would for
|
2013-03-04 20:54:34 +01:00
|
|
|
an anchor tag. Examples:
|
|
|
|
|
|
|
|
url.resolve('/one/two/three', 'four') // '/one/two/four'
|
|
|
|
url.resolve('http://example.com/', '/one') // 'http://example.com/one'
|
|
|
|
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
|