[d24f17c] | 1 | # url-parse
|
---|
| 2 |
|
---|
| 3 | [](https://www.npmjs.com/package/url-parse)[](https://github.com/unshiftio/url-parse/actions?query=workflow%3ACI+branch%3Amaster)[](https://coveralls.io/r/unshiftio/url-parse?branch=master)
|
---|
| 4 |
|
---|
| 5 | [](https://saucelabs.com/u/url-parse)
|
---|
| 6 |
|
---|
| 7 | **`url-parse` was created in 2014 when the WHATWG URL API was not available in
|
---|
| 8 | Node.js and the `URL` interface was supported only in some browsers. Today this
|
---|
| 9 | is no longer true. The `URL` interface is available in all supported Node.js
|
---|
| 10 | release lines and basically all browsers. Consider using it for better security
|
---|
| 11 | and accuracy.**
|
---|
| 12 |
|
---|
| 13 | The `url-parse` method exposes two different API interfaces. The
|
---|
| 14 | [`url`](https://nodejs.org/api/url.html) interface that you know from Node.js
|
---|
| 15 | and the new [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL)
|
---|
| 16 | interface that is available in the latest browsers.
|
---|
| 17 |
|
---|
| 18 | In version `0.1` we moved from a DOM based parsing solution, using the `<a>`
|
---|
| 19 | element, to a full Regular Expression solution. The main reason for this was
|
---|
| 20 | to make the URL parser available in different JavaScript environments as you
|
---|
| 21 | don't always have access to the DOM. An example of such environment is the
|
---|
| 22 | [`Worker`](https://developer.mozilla.org/en/docs/Web/API/Worker) interface.
|
---|
| 23 | The RegExp based solution didn't work well as it required a lot of lookups
|
---|
| 24 | causing major problems in FireFox. In version `1.0.0` we ditched the RegExp
|
---|
| 25 | based solution in favor of a pure string parsing solution which chops up the
|
---|
| 26 | URL into smaller pieces. This module still has a really small footprint as it
|
---|
| 27 | has been designed to be used on the client side.
|
---|
| 28 |
|
---|
| 29 | In addition to URL parsing we also expose the bundled `querystringify` module.
|
---|
| 30 |
|
---|
| 31 | ## Installation
|
---|
| 32 |
|
---|
| 33 | This module is designed to be used using either browserify or Node.js it's
|
---|
| 34 | released in the public npm registry and can be installed using:
|
---|
| 35 |
|
---|
| 36 | ```
|
---|
| 37 | npm install url-parse
|
---|
| 38 | ```
|
---|
| 39 |
|
---|
| 40 | ## Usage
|
---|
| 41 |
|
---|
| 42 | All examples assume that this library is bootstrapped using:
|
---|
| 43 |
|
---|
| 44 | ```js
|
---|
| 45 | 'use strict';
|
---|
| 46 |
|
---|
| 47 | var Url = require('url-parse');
|
---|
| 48 | ```
|
---|
| 49 |
|
---|
| 50 | To parse an URL simply call the `URL` method with the URL that needs to be
|
---|
| 51 | transformed into an object.
|
---|
| 52 |
|
---|
| 53 | ```js
|
---|
| 54 | var url = new Url('https://github.com/foo/bar');
|
---|
| 55 | ```
|
---|
| 56 |
|
---|
| 57 | The `new` keyword is optional but it will save you an extra function invocation.
|
---|
| 58 | The constructor takes the following arguments:
|
---|
| 59 |
|
---|
| 60 | - `url` (`String`): A string representing an absolute or relative URL.
|
---|
| 61 | - `baseURL` (`Object` | `String`): An object or string representing
|
---|
| 62 | the base URL to use in case `url` is a relative URL. This argument is
|
---|
| 63 | optional and defaults to [`location`](https://developer.mozilla.org/en-US/docs/Web/API/Location)
|
---|
| 64 | in the browser.
|
---|
| 65 | - `parser` (`Boolean` | `Function`): This argument is optional and specifies
|
---|
| 66 | how to parse the query string. By default it is `false` so the query string
|
---|
| 67 | is not parsed. If you pass `true` the query string is parsed using the
|
---|
| 68 | embedded `querystringify` module. If you pass a function the query string
|
---|
| 69 | will be parsed using this function.
|
---|
| 70 |
|
---|
| 71 | As said above we also support the Node.js interface so you can also use the
|
---|
| 72 | library in this way:
|
---|
| 73 |
|
---|
| 74 | ```js
|
---|
| 75 | 'use strict';
|
---|
| 76 |
|
---|
| 77 | var parse = require('url-parse')
|
---|
| 78 | , url = parse('https://github.com/foo/bar', true);
|
---|
| 79 | ```
|
---|
| 80 |
|
---|
| 81 | The returned `url` instance contains the following properties:
|
---|
| 82 |
|
---|
| 83 | - `protocol`: The protocol scheme of the URL (e.g. `http:`).
|
---|
| 84 | - `slashes`: A boolean which indicates whether the `protocol` is followed by two
|
---|
| 85 | forward slashes (`//`).
|
---|
| 86 | - `auth`: Authentication information portion (e.g. `username:password`).
|
---|
| 87 | - `username`: Username of basic authentication.
|
---|
| 88 | - `password`: Password of basic authentication.
|
---|
| 89 | - `host`: Host name with port number. The hostname might be invalid.
|
---|
| 90 | - `hostname`: Host name without port number. This might be an invalid hostname.
|
---|
| 91 | - `port`: Optional port number.
|
---|
| 92 | - `pathname`: URL path.
|
---|
| 93 | - `query`: Parsed object containing query string, unless parsing is set to false.
|
---|
| 94 | - `hash`: The "fragment" portion of the URL including the pound-sign (`#`).
|
---|
| 95 | - `href`: The full URL.
|
---|
| 96 | - `origin`: The origin of the URL.
|
---|
| 97 |
|
---|
| 98 | Note that when `url-parse` is used in a browser environment, it will default to
|
---|
| 99 | using the browser's current window location as the base URL when parsing all
|
---|
| 100 | inputs. To parse an input independently of the browser's current URL (e.g. for
|
---|
| 101 | functionality parity with the library in a Node environment), pass an empty
|
---|
| 102 | location object as the second parameter:
|
---|
| 103 |
|
---|
| 104 | ```js
|
---|
| 105 | var parse = require('url-parse');
|
---|
| 106 | parse('hostname', {});
|
---|
| 107 | ```
|
---|
| 108 |
|
---|
| 109 | ### Url.set(key, value)
|
---|
| 110 |
|
---|
| 111 | A simple helper function to change parts of the URL and propagating it through
|
---|
| 112 | all properties. When you set a new `host` you want the same value to be applied
|
---|
| 113 | to `port` if has a different port number, `hostname` so it has a correct name
|
---|
| 114 | again and `href` so you have a complete URL.
|
---|
| 115 |
|
---|
| 116 | ```js
|
---|
| 117 | var parsed = parse('http://google.com/parse-things');
|
---|
| 118 |
|
---|
| 119 | parsed.set('hostname', 'yahoo.com');
|
---|
| 120 | console.log(parsed.href); // http://yahoo.com/parse-things
|
---|
| 121 | ```
|
---|
| 122 |
|
---|
| 123 | It's aware of default ports so you cannot set a port 80 on an URL which has
|
---|
| 124 | `http` as protocol.
|
---|
| 125 |
|
---|
| 126 | ### Url.toString()
|
---|
| 127 |
|
---|
| 128 | The returned `url` object comes with a custom `toString` method which will
|
---|
| 129 | generate a full URL again when called. The method accepts an extra function
|
---|
| 130 | which will stringify the query string for you. If you don't supply a function we
|
---|
| 131 | will use our default method.
|
---|
| 132 |
|
---|
| 133 | ```js
|
---|
| 134 | var location = url.toString(); // http://example.com/whatever/?qs=32
|
---|
| 135 | ```
|
---|
| 136 |
|
---|
| 137 | You would rarely need to use this method as the full URL is also available as
|
---|
| 138 | `href` property. If you are using the `URL.set` method to make changes, this
|
---|
| 139 | will automatically update.
|
---|
| 140 |
|
---|
| 141 | ## Testing
|
---|
| 142 |
|
---|
| 143 | The testing of this module is done in 3 different ways:
|
---|
| 144 |
|
---|
| 145 | 1. We have unit tests that run under Node.js. You can run these tests with the
|
---|
| 146 | `npm test` command.
|
---|
| 147 | 2. Code coverage can be run manually using `npm run coverage`.
|
---|
| 148 | 3. For browser testing we use Sauce Labs and `zuul`. You can run browser tests
|
---|
| 149 | using the `npm run test-browser` command.
|
---|
| 150 |
|
---|
| 151 | ## License
|
---|
| 152 |
|
---|
| 153 | [MIT](LICENSE)
|
---|