1.0.3 • Published 7 years ago
parse-link v1.0.3
parse-link
Unified URL parsing API in the browser and Node.
Installation
npm:
npm install parse-linkUsage
var parse = require('parse-link')
console.log(parse('http://user:pass@host.com: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:pass@host.com: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) —hostnamewith portport(number) — TCP portsearch(string) —querywith question markquery(string) — GET parametershash(string) — Hash, including pound/octothorp/what-evs
Node supports a second parameter which the given
urlisrelativeto: 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.