Node.js url.parse() and pathname property

pathname is the path section of the URL, that comes after the host and before the query, including the initial slash if present.

For example:

url.parse('http://stackoverflow.com/questions/17184791').pathname    

will give you:

"/questions/17184791"

Here's an example:

var url = "https://u:[email protected]:777/a/b?c=d&e=f#g";
var parsedUrl = require('url').parse(url);
...
protocol  https:
auth      u:p
host      www.example.com:777
port      777
hostname  www.example.com
hash      #g
search    ?c=d&e=f
query     c=d&e=f
pathname  /a/b
path      /a/b?c=d&e=f
href      https://www.example.com:777/a/b?c=d&e=f#g

And another:

var url = "http://example.com/";
var parsedUrl = require('url').parse(url);
...
protocol http:
auth     null
host     example.com
port     null
hostname example.com
hash     null
search   null
query    null
pathname /
path     /
href     http://example.com/

Node.js docs: URL Objects

Update for NodeJS 11+

Starting in Node.js 11, url.parse was deprecated in favor of using the URL class which follows the WHATWG standard. Parsing is very similar but a few properties have changed:

const { URL } = require('url');
const url = "https://u:[email protected]:777/a/b?c=d&e=f#g";
const parsedUrl = new URL(url);
...
href         https://u:[email protected]:777/a/b?c=d&e=f#g
origin       https://www.example.com:777
protocol     https:
username     u
password     p
host         www.example.com:777
hostname     www.example.com
port         777
pathname     /a/b
search       ?c=d&e=f
searchParams { 'c' => 'd', 'e' => 'f' }
hash         #g

Tags:

Node.Js