What is url-parse?
The url-parse package is a robust tool for parsing URLs in Node.js and browser environments. It provides a convenient way to break down a URL into its components, such as protocol, host, path, query parameters, and hash. This package is useful for applications that need to manipulate or extract information from URLs.
What are url-parse's main functionalities?
Parsing URL
This feature allows you to parse a full URL into its constituent parts, including protocol, username, password, host, port, pathname, query, and hash. The second parameter set to true parses the query string into an object.
const parse = require('url-parse');
const url = parse('http://username:password@host.com:8080/p/a/t/h?query=string#hash', true);
console.log(url.protocol); // 'http:'
console.log(url.host); // 'host.com:8080'
Manipulating Query Strings
This feature demonstrates how to manipulate query strings. After parsing the URL with the query string parsing option enabled, you can easily add, modify, or delete query parameters and then serialize the URL back to a string.
const parse = require('url-parse');
const url = parse('http://example.com?foo=bar', true);
url.query.newParam = 'newValue';
console.log(url.toString()); // 'http://example.com/?foo=bar&newParam=newValue'
Relative URL Resolution
This feature shows how to resolve relative URLs against a base URL. By parsing both the base and relative URLs, you can combine their components to form a new, resolved URL.
const parse = require('url-parse');
const baseUrl = parse('http://example.com/directory/');
const relativeUrl = parse('another/directory', true);
const resolvedUrl = baseUrl.set('pathname', baseUrl.pathname + relativeUrl.pathname);
console.log(resolvedUrl.toString()); // 'http://example.com/directory/another/directory'
Other packages similar to url-parse
whatwg-url
This package implements the URL standard as specified by the WHATWG (Web Hypertext Application Technology Working Group). It offers more comprehensive support for the URL standard than url-parse, including features like URLSearchParams. However, it might be more complex to use for simple URL parsing and manipulation tasks.
url-parse
When required on node it will expose the url
module's .parse
method. When
required in the browser it will offload the URL parsing to the <a>
element in
the DOM. This allows the module to be really tiny on the browser and still be
usable on node.
In addition to parsing URL's it also has a really simple query string parser and
query string stringify.
Installation
This module is designed to be used using either browserify or node.js it's
released in the public npm registry and can be installed using:
npm install url-parse
Usage
All examples assume that this library is bootstrapped using:
'use strict';
var parse = require('url-parse');
To parse an URL simply call the parse
method with the URL that needs to be
transformed in to an object.
var url = parse('https://github.com/foo/bar');
The URL should now be somewhat the same as the node.js's url.parse
output. The
notable exception being that in the browser not all properties would be set to
null.
parse.querystring()
Parse the given query string and return an object representation from it.
parse.querystring('foo=bar&bar=foo');
parse.querystring('?foo=bar&bar=foo');
parse.querystringify()
Take an object and make a query string from it.
parse.querystringify({ foo: 'bar' });
parse.querystringify({ foo: 'bar' }, true);
parse.querystringify({ foo: 'bar' }, '&');
License
MIT