Index
Overview


A video that accompanies the page is here....

For our purposes, we'll start with these abridged defintions:


Where this agreement is taken care of "behind the scenes" (by web server and web browser)

On this page, we'll identify key URL components for web programming.

URL Components


The five components of a uniform resource locator (url):

⚠️ keep it simple warning -- different sources could use different terminology -- don't get hung up on that.

https://www.abc.com/shapes/rectangle?width=10&height=2#conclusion


ComponentExampleAlso Called
schemehttpsprotocol
hostwww.abc.comserver id or domain name
path/shapes/rectangleor folder name + web filename
querywidth=10&height=2query params or search params
fragment identifierconclusionfragment - often used to "go to" a section heading within a page.


URL Key Components


The path and query are of primary interest to web programmers.

When coding a web server, we are mainly interested in the path and query of the url.
https://www.abc.com/shapes/rectangle?width=10&height=2


NameExampleAlso Called
path/shapes/rectanglefolder name + web filename
querywidth=10&height=2query params or search params

The path allows us to determine a route for our processing
ComponentDescription
/An empty path "/" might route us to "home" or "index" page processing
/calcAny other path will drive us to app specific logic

We use the query as data related to our processing
ComponentDescription
width=10&height=2The parameters might provide model values
search=url&results=5The parameters might tell us to do a search with a max of five results


Parsing Path And Query Params


Head over here to learn the details of node's URL global object.

Or, we can use a simpler "wrapper object" that simplifies the URL object as shown in the following.

Install url-wrapper
After initializing the project, install the "url-wrapper" module shown
> npm install url-wrapper
Importing
Import the wrapper class as shown
UrlWrapper = require('url-wrapper').UrlWrapper;
Constructing
Construct the wrapper as shown (using a request object).
urlWrapper = UrlWrapper.fromRequest(req);
Usage
For the following usage steps, we'll assume we call the server with the URL show.
http://localhost:3000/shapes/rectangle?width=10&height=5
Path
Use the "path" method to get the url path.
path = urlWrapper.path();
//	/shapes/rectangle
Query Params
Use the "query" method to get the query params as a plain object / map.

Then use it as shown.
query = urlWrapper.query();
w = query.width;
//10
h = query['height'];
//2


More References