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(); }
URL Parsing. The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With