<pre><code>var URL = require('url').URL;
var myURL = new URL('http://www.example.com/foo?bar=1#main');

console.log(myURL.host);

// prints 'www.example.com'

</code></pre>

<p>Here is an overview of all the different parts of an URL and how the according <code>URL</code> object attribute is named:</p>

<pre><code>&quot;  https:   //  user : pass @ sub.host.com : 80   /p/a/t/h  ?  query=string   #hash &quot;
│          │  │      │      │   hostname   │port│          │                │     