What is parseuri?
The parseuri npm package is a simple utility for parsing URIs (Uniform Resource Identifiers). It breaks down a URI string into its components, such as protocol, host, port, path, query, and fragment.
What are parseuri's main functionalities?
Parse a URI
This feature allows you to parse a URI string into its components. The code sample demonstrates how to use the parseuri package to break down a URI into its parts, such as protocol, host, port, path, query, and fragment.
const parseuri = require('parseuri');
const uri = 'http://example.com:8080/path?query=string#fragment';
const parsed = parseuri(uri);
console.log(parsed);
Other packages similar to parseuri
url-parse
The url-parse package is a robust URL parser that works in both Node.js and the browser. It provides similar functionality to parseuri but includes additional features like URL normalization and support for IPv6 addresses.
query-string
The query-string package focuses on parsing and stringifying URL query strings. While it doesn't parse the entire URI, it complements packages like parseuri by handling the query string component specifically.
whatwg-url
The whatwg-url package is a full implementation of the WHATWG URL Standard, providing comprehensive URL parsing and manipulation capabilities. It is more feature-rich compared to parseuri and adheres to modern web standards.
parseUri 3.0.1
parseUri
is a mighty but tiny JavaScript URI/URN/URL parser that splits any URI into its parts (all of which are optional). Its combination of accuracy, comprehensiveness, and brevity is unrivaled (1KB min/gzip, with no dependencies).
Compared to the built-in URL
parseUri
includes several advantages over URL
:
parseUri
gives you many additional properties (authority
, userinfo
, subdomain
, domain
, tld
, resource
, directory
, filename
, suffix
) that aren’t available from URL
.URL
throws e.g. if not given a protocol, and in many other cases of valid (but not supported) and invalid URIs. parseUri
makes a best case effort even with partial or invalid URIs and is extremely good with edge cases.URL
’s rules don’t allow correctly handling many non-web protocols. For example, URL
doesn’t throw on any of 'git://localhost:1234'
, 'ssh://myid@192.168.1.101'
, or 't2ab:///path/entry'
, but it also doesn’t get their details correct since it treats everything after <non-web-protocol>:
up to ?
or #
as part of the pathname
.parseUri
includes a “friendly” parsing mode (in addition to its default mode) that handles human-friendly URLs like 'example.com/file.html'
as expected.parseUri
supports providing a list of second-level domains that should be treated as part of the top-level domain (ex: co.uk
).
Conversely, parseUri
is single-purpose and doesn’t apply normalization. You can of course use a URI normalizer separately, or build one on top of parseUri
.
parseUri
’s demo page allows easily comparing with URL
’s results.
Results / URI parts
Returns an object with 20 URI parts as properties plus queryParams
, a URLSearchParams
object that includes methods get(key)
, getAll(key)
, etc.
Here’s an example of what each part contains:
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├────────────────────────────────────────────────────────────────┬─────────────────────────────────────────┤
│ origin │ resource │
├──────────┬─┬───────────────────────────────────────────────────┼──────────────────────┬───────┬──────────┤
│ protocol │ │ authority │ pathname │ query │ fragment │
│ │ ├─────────────────────┬─────────────────────────────┼───────────┬──────────┤ │ │
│ │ │ userinfo │ host │ directory │ filename │ │ │
│ │ ├──────────┬──────────┼──────────────────────┬──────┤ ├─┬────────┤ │ │
│ │ │ username │ password │ hostname │ port │ │ │ suffix │ │ │
│ │ │ │ ├───────────┬──────────┤ │ │ ├────────┤ │ │
│ │ │ │ │ subdomain │ domain │ │ │ │ │ │ │
│ │ │ │ │ ├────┬─────┤ │ │ │ │ │ │
│ │ │ │ │ │ │ tld │ │ │ │ │ │ │
" https :// user : pass @ sub1.sub2 . dom.com : 8080 /p/a/t/h/ a.html ? q=1 # hash "
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
parseUri
additionally supports IPv4 and IPv6 addresses, URNs, and many edge cases not shown here. See tests.
Parsing modes
parseUri
has two parsing modes: default and friendly. The default mode follows official URI rules. Friendly mode doesn’t require '<protocol>:'
, ':'
, or '//'
to signal the start of an authority, which allows handling human-friendly URLs like 'example.com/file.html'
as expected. This change has several effects:
- It allows starting a URI with an authority (as noted above).
- It precludes friendly mode from properly handling relative paths (that don’t start from root
'/'
) such as 'dir/file.html'
. - Since the web protocols
http
, https
, ws
, wss
, and ftp
don’t require '//'
, this also means that friendly mode extends this behavior to non-web protocols.
Usage examples
let uri = parseUri('https://a.b.example.com:80/@user/a/my.img.jpg?q=x&q=#hash');
uri.protocol
uri.host
uri.hostname
uri.subdomain
uri.domain
uri.port
uri.resource
uri.pathname
uri.directory
uri.filename
uri.suffix
uri.query
uri.fragment
uri.queryParams.get('q')
uri.queryParams.getAll('q')
uri.queryParams.get('not-present')
uri.queryParams.getAll('not-present')
uri = parseUri('dir/file.html?q=x');
uri.hostname
uri.directory
uri.filename
uri.query
uri = parseUri('example.com/file.html', 'friendly');
uri.hostname
uri.directory
uri.filename
uri = parseUri('ssh://myid@192.168.1.101');
uri.protocol
uri.username
uri.hostname
uri.domain
uri = parseUri('https://[2001:db8:85a3::7334]:80?q=x');
uri.hostname
uri.port
uri.domain
uri.query
uri = parseUri('mailto:first@my.com,second@my.com?subject=Hey&body=Sign%20me%20up!');
uri.protocol
uri.username
uri.hostname
uri.pathname
uri.query
uri.queryParams.get('body')
uri = parseUri('mailto:me@my.com?subject=Hey', 'friendly');
uri.protocol
uri.username
uri.hostname
uri.pathname
Use parseUri
’s demo page to easily test and compare results.