0

So I'm trying to parse host string ("asd.com:123" for example) to obtain domain name and port. It looks like node.js has build-in URL module just for this. However it doesn't work as I expect (maybe it's supposed to parse only fully-qualified URLs?). Is there any other way to perform this?

var urlModule = require('url');
var stringToParse = 'asd.com:123';
var parseResult = urlModule.parse(stringToParse);
console.log(parseResult);

output:

{ protocol: 'asd.com:',
slashes: null,
auth: null,
host: '123',
port: null,
hostname: '123',
hash: null,
search: null,
query: null,
pathname: null,
path: null,
href: 'asd.com:123' }

1 Answer 1

1

Strictly speaking, I think asd.com:123 is a valid URL is, as in for example the file URL file:filename

Can you simply add 'http://' to stringToParse?

var parseResult = urlModule.parse('http://' + stringToParse);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.