45

I'm reading a getting started book on node.js called The Node Beginner Book and in the code below (given in the book) I don't understand the significance of the pathname property hanging off the parse method. So I would like to know what it is doing. The documentation for this method is not clear to me

var pathname = url.parse(request.url)**.pathname;** 

var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;         // I don't understand the pathname property
    console.log("Request for " + pathname + " received.");
    route(handle, pathname);
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}

5 Answers 5

62

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"
5
31

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
1
  • is "https:" with a colon really the correct protocol string, instead of just "https"?
    – sezanzeb
    Jan 26, 2023 at 15:55
0

pathname is the part of URL section that comes after server and port. In,var pathname = url.parse(request.url).pathname; the request.url ,requests the url from the URL Section which is the set of the component - IP address of localhost , port no and file pathname.

Let understand it by an example suppose this is the url to be requested to server http://127.0.0.1:8082/ but for response to the client there should be an html file let it be index.html then http://127.0.0.1:8080/index.html and this html file is the .pathname to the url. So,In var pathname = url.parse(http://127.0.0.1:8080/index.html).pathname the pathname is index.html that is response to client.

0
url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

urlString: The URL string to parse.

parseQueryString : If true, the query property will always be set to an object returned by the querystring module's parse() method.

slashesDenoteHost : If true, the first token after the literal string // and preceding the next / will be interpreted as the host

So, the url.parse() method takes a URL string, parses it, and returns a URL object.

Thus,

var pathname = url.parse(request.url).pathname;

will return the path name of the host followed by '/'

For example:

var pathname = url.parse(https://nodejs.org/docs/latest/api/url.html).pathname

will return:

/docs//latest/api/url.html
0

if the following url is redirected in nodejs "http://localhost:9090/page/edit?pageId=1&type=edit"

q.pathname will be "/page/edit" section of the URL. Please find other section of

http.createServer(function (req, res) {
 var q = url.parse(req.url, true);
 console.log(q.pathname);
 // /page/edit
 console.log(q.query['type'])
 // edit
 console.log(q)
 //will show below attached image
})

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.