How can I allocate URL objects in node.js?

Tag: node.js Author: zlzcgf Date: 2010-08-26

How can I allocate an instance of a URL object using node.js from an existing url string?

Something like this:

var url = require('url');
var myurl = new url("");

I can't seem to find any mention/example of this anywhere.

Best Answer

var url = require('url');
var myurl = url.parse('');

You can now use

myurl.hostname //
myurl.pathname // /blah

and so on..

You very rarely (if ever) need to use the new keyword in relation to the built-in modules, as long as you use the documented functions.


Perfect. Thanks!