https://www.abc.com/rectangle?width=10&height=2
Name | Example |
---|---|
path | /rectangle |
query | width=10&height=2 |
base = 'http://' + req.headers.host + '/'; urlPathAndQuery = req.url; url = new URL(urlPathAndQuery, base);
path = url.pathname() queryString = url.search; //query as key-value pairs queryPairs = Array.from(url.searchParams);
//given "queryPairs" from previous step. url.searchParams const plainObject = {}; for (let pair of queryPairs) plainObject[pair[0]] = pair[1];
//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"];
class UrlWrapper { constructor(req) path() queryAsString() queryAsArray() queryAsPlainObject() }