parse-link
v1.0.3
Published
Unified URL parsing API in the browser and node
Downloads
68
Readme
parse-link
Unified URL parsing API in the browser and Node.
Installation
npm:
npm install parse-link
Usage
var parse = require('parse-link')
console.log(parse('http://user:[email protected]:8080/path?query=string#hash'))
Yields:
{
protocol: 'http:',
host: 'host.com:8080',
port: 8080,
hostname: 'host.com',
hash: '#hash',
search: '?query=string',
query: 'query=string',
pathname: '/path',
href: 'http://user:[email protected]:8080/path?query=string#hash' }
Node also supports some more properties, like
path
("/path?query=string"
) andauth
("user:pass"
).
API
parse(url)
Parses url
(string
). Uses anchor tags in the browser, and Node’s
URL module in Node.
Returns
Object
:
href
(string) — given (cleaned) urlpathname
(string) — pathprotocol
(string) —http:
,https:
,mailto:
, etchostname
(string) — complete domain, including subdomains andwww.
host
(string) —hostname
with portport
(number) — TCP portsearch
(string) —query
with question markquery
(string) — GET parametershash
(string) — Hash, including pound/octothorp/what-evs
Node supports a second parameter which the given
url
isrelative
to: In the browser, URLs are parsed relative towindow.location
, whereas in Node such a thing doesn’t exist.
Todo
There are still some slight differences between the two interfaces: mostly in non-http settings, or with relative links. I’d like to make them look more alike.