URL Object
We'll utilize the Node.js global object "URL".
new URL
path and query params
We want to get the path and the query from the URL.

So we need to parse the URL string.
https://www.abc.com/rectangle?width=10&height=2

NameExample
path/rectangle
querywidth=10&height=2
Parsing Using WHATWG URL API
Node.js provides a url module for parsing the URL.

It includes a URL class that follows the WHATWG URL API.

Note: The 'url' module 'parse' function has been deprecated in favor of the URL object.


WHATWG URL API
Constructing URL Object
Given a request object (var 'req') we can construct the Node.js URL object as shown.
base = 'http://' + req.headers.host + '/';
urlPathAndQuery = req.url;
url = new URL(urlPathAndQuery, base);
Using URL Object
Here we show the URL methods that provide what we need (path and query)

Note we convert the unique URL pairs collection to a familiar Array type for ease of use.
path = url.pathname()
queryString = url.search;
//query as key-value pairs
queryPairs = Array.from(url.searchParams);
Converting Query to Plain Object
We know Js plain objects are user friendly.

So here we convert the URL query pairs to a plain object.
//given "queryPairs" from previous step.
url.searchParams
const plainObject = {};
for (let pair of queryPairs)
	plainObject[pair[0]] = pair[1];
Using the Plain Object
Here is example usage of our plain object.

As shown we are off to the races in terms of using the query params.
//given "plainObject" from the previous step
//assuming our query param keys are "width" and "height"
w = plainObject.width;
h = plainObject.height;
//or remember plain objects can be used like maps
w = plainObject["width"];
h = plainObject["height"];
Simplify With Encapsulation
The above steps suggest to us that this node.js URL object is a bit complex.

This is a nice opportunity for a little "wrapper" object (class) to encapsulate the complexity and provide just what we need (path and query).

Also see wrapper library pattern.

An example shell of a possible wrapper class is shown.
class UrlWrapper {
	constructor(req)
	path()
	queryAsString()
	queryAsArray()
	queryAsPlainObject()
}