Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js url.parse() and pathname property

Tags:

node.js

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(); } 
like image 769
William Avatar asked Jun 19 '13 06:06

William


People also ask

What is URL parse ()?

URL Parsing. The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string.

Which method can you used to parse an address with the URL?

The url. parse() method takes a URL string, parses it, and it will return a URL object with each part of the address as properties.

What is URL pathname?

A URL (Uniform Resource Locator) identifies a resource on a remote server and gives the network location on that server. The URL path is the string of information that comes after the top level domain name. You can use the HTTP-proxy to block websites that contain specified text in the URL path.


1 Answers

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" 
like image 147
sohel khalifa Avatar answered Oct 04 '22 16:10

sohel khalifa