url-parse
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
querystringify | 2.2.0 | 3.03 kB | MIT | prod | |
requires-port | 1.0.0 | 2.99 kB | MIT | prod |
"url-parse" is a lightweight JavaScript library that provides efficient and versatile URL parsing across Node.js and browser environments. This utility was initially designed when the WHATWG URL API was not supported in Node.js, and the URL interface was available only in certain browsers. Now, though, this package offers two different API interfaces: the familiar URL interface from Node.js, and the newer URL interface available in the latest browsers. As well as parsing URLs, url-parse also makes the packaged "querystringify" module available. This tool, with its small footprint, is particularly suited to client-side use, offering a reliable URL parsing solution for different JavaScript environments such as the Worker interface.
To use url-parse in your application, you first need to install it using npm with the command npm install url-parse
. Once installed, you can import it in your Node.js file using require
:
var Url = require('url-parse');
You can then parse a URL by creating a new instance of the Url
object and passing the URL string as an argument. This can be an absolute or relative URL.
var url = new Url('https://github.com/foo/bar');
If necessary, you can also specify a base URL and a query string parser.
In addition to the above method, url-parse also supports the Node.js interface, so you can use the library in this way:
var parse = require('url-parse'),
url = parse('https://github.com/foo/bar', true);
The parsed URL instance will contain several properties such as protocol, hostname, port, pathname, and query.
To modify parts of the URL and propagate the changes through all properties, you can use the set
method:
var parsed = parse('http://google.com/parse-things');
parsed.set('hostname', 'yahoo.com');
console.log(parsed.href); // outputs: http://yahoo.com/parse-things
Finally, to generate a full URL from the parsed object, you can use the toString
method:
var location = url.toString(); // outputs: http://example.com/whatever/?qs=32
The complete documentation for url-parse is available on the package's GitHub page, which you can visit at: https://github.com/unshiftio/url-parse. Here, you will find detailed information on installation, usage, testing, and licensing, in addition to the full readme content. The npm registry entry for the package also includes links to the GitHub repository and other relevant resources.