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("http://google.com/blah");

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

Best Answer

var url = require('url');
var myurl = url.parse('http://google.com/blah');

You can now use

myurl.hostname // google.com
myurl.pathname // /blah

and so on..

http://nodejs.org/api.html#url-302

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.

comments:

Perfect. Thanks!