node.js - NodeJS URL parsing results wrong -
here code:
var url = require('url'); console.log(url.parse('localhost:3000'));
results in output follows:
{ protocol: 'localhost:', host: '3000', hostname: '3000', href: 'localhost:3000' }
very weird. guess because of no existence of 'http' @ beginning. such case, expecting like:
{ port: '3000', host: 'localhost', hostname: 'localhost', href: 'localhost:3000' }
any third party libraries or suggestion achieve this?
strictly speaking passed in not valid url. according rfc 3986, scheme not optional:
uri = scheme ":" hier-part [ "?" query ] [ "#" fragment ] hier-part = "//" authority path-abempty / path-absolute / path-rootless / path-empty
having said that, of course node.js have warned invalid url in case.
in opinion, in such trivial case, instead of searching additional library, might better write couple lines of code fix url first, before passing parser.
furthermore, in particular case, solely based on above definition, parser has every reason understand localhost scheme, instead of host.
Comments
Post a Comment