What is parse-url?
The parse-url npm package is used to parse URLs and extract components such as protocol, auth, host, pathname, query parameters, and hash. It provides a simple API to break down a URL string into its constituent parts for easy access and manipulation.
What are parse-url's main functionalities?
Parsing the URL
This feature allows you to parse a full URL and returns an object with properties like protocol, resource, user, pathname, hash, search, and query. Each property represents a specific part of the URL.
const parseUrl = require('parse-url');
const parsed = parseUrl('https://www.example.com:8080/path/name?query=value#hash');
console.log(parsed);
Parsing the Query String
This feature specifically focuses on parsing the query string part of the URL and converting it into an object for easy access to each query parameter.
const parseUrl = require('parse-url');
const parsed = parseUrl('https://www.example.com/path/name?query=value&another=thing');
console.log(parsed.query);
Reconstructing the URL
After parsing a URL and potentially modifying its components, this feature allows you to reconstruct the URL back into a string.
const parseUrl = require('parse-url');
const parsed = parseUrl('https://www.example.com/path/name');
parsed.resource = 'www.another-example.com';
const reconstructedUrl = parseUrl.stringify(parsed);
console.log(reconstructedUrl);
Other packages similar to parse-url
url-parse
The url-parse package offers similar functionality to parse-url, allowing for the parsing of URLs into components. It also provides the ability to resolve relative URLs against an absolute base URL, which is a feature not present in parse-url.
qs
While qs is not a direct alternative to parse-url, it provides robust query string parsing and stringifying capabilities. It can be used in conjunction with other URL parsing libraries to handle the query string part of URLs more extensively than parse-url.
urijs
URI.js is a URL manipulation library that provides a fluent API for building, parsing, and manipulating URLs. It offers more features than parse-url, such as URL normalization, resolution of relative paths, and support for punycode and IPv6.
parse-url
An advanced url parser supporting git urls too.
:cloud: Installation
$ npm i --save parse-url
:clipboard: Example
const parseUrl = require("parse-url");
console.log(parseUrl("http://ionicabizau.net/blog"));
console.log(parseUrl("http://domain.com/path/name?foo=bar&bar=42#some-hash"));
console.log(parseUrl("git+ssh://git@host.xz/path/name.git"));
console.log(parseUrl("git@github.com:IonicaBizau/git-stats.git"));
:memo: Documentation
parseUrl(url)
Parses the input url.
Params
- String
url
: The input url.
Return
- Object An object containing the following fields:
protocols
(Array): An array with the url protocols (usually it has one element).protocol
(String): The first protocol, "ssh"
(if the url is a ssh url) or "file"
.port
(null|Number): The domain port.resource
(String): The url domain (including subdomains).user
(String): The authentication user (usually for ssh urls).pathname
(String): The url pathname.hash
(String): The url hash.search
(String): The url querystring value.href
(String): The input url.
:yum: How to contribute
Have an idea? Found a bug? See how to contribute.
:dizzy: Where is this library used?
If you are using this library in one of your projects, add it in this list. :sparkles:
deploy-versioning
—Deploy your code keeping older versions.git-up
—A low level git url parser.hubot-will-it-connect
(by gambtho)—Connects hubot with willitconnect, to validate CF's ability to connect to external resourceskakapo
(by devlucky)—Next generation mocking framework in Javascriptlien
—Another lightweight NodeJS framework. Lien is the link between request and response objects.microbe.js
(by Brandon Dail)—A small Node.js framework for simple routingtumblr-text
(by cobaimelan)— [](https://david-dm.org/ayhanurl-local
—Checks if a given url is a local url or not.
:scroll: License
MIT © Ionică Bizău