unshiftio / url-parse

Small footprint URL parser that works seamlessly across Node.js and browser environments.
http://unshift.io
MIT License
1.03k stars 104 forks source link

Url with no protocol ends up being localhost #196

Closed mchen391 closed 4 years ago

mchen391 commented 4 years ago

Version: 1.4.7

When I try to parse url without protocol specified like test.com, example.com or example.com/path, the host will be localhost and the input will be considered as path. For example,

url = new Url("example.com");
console.log(url)

results in

    {
      slashes: true,
      protocol: 'http:',
      hash: '',
      query: '',
      pathname: '/example.com',
      auth: '',
      host: 'localhost',
      port: '',
      hostname: 'localhost',
      password: '',
      username: '',
      origin: 'http://localhost',
      href: 'http://localhost/example.com'
    }

I think it is due to the baseUrl being location and the input is considered as a relative url. Is there an option to specify that it's an absolute url?

lpinca commented 4 years ago

You can use an empty object for the baseURL argument:

new Url('example.com', {})
mchen391 commented 4 years ago

Thanks! I would love to see it in documentation though. Apology if it does exist in documentation and I missed that