Learn

Typically, an HTTP server will require information from the request URL to accurately process a request. This request URL is located on the url property contained within the req object itself. To parse the different parts of this URL easily, Node.js provides the built-in url module. The core of the url module revolves around the URL class. A new URL object can be instantiated using the URL class as follows:

const url = new URL('https://www.example.com/p/a/t/h?query=string');

Once instantiated, different parts of the URL can be accessed and modified via various properties, which include:

  • hostname: Gets and sets the host name portion of the URL.
  • pathname: Gets and sets the path portion of the URL.
  • searchParams: Gets the search parameter object representing the query parameters contained within the URL. Returns an instance of the URLSearchParams class.

You might recognize the URL and URLSearchParams classes if you are familiar with browser-based JavaScript. It’s because they are actually the same thing! These classes are defined by the WHATWG URL specification. Both the browser and Node.js implement this API, which means developers can have a similar developer experience working with both client and server-side JavaScript.

Using these properties, one can break the URL down into easily usable parts for processing the request.

const host = url.hostname; // example.com const pathname = url.pathname; // /p/a/t/h const searchParams = url.searchParams; // {query: 'string'}

While the url module can be used to deconstruct a URL into its constituent parts, it can also be used to construct a URL. Constructing a URL via this method relies on most of the same properties listed above to set values on the URL instead of retrieving them. This can be done by setting each of these values equal to a value for the newly constructed URL. Once all parts of the URL have been added, the composed URL can be obtained using the .toString() method.

const createdUrl = new URL('https://www.example.com'); createdUrl.pathname = '/p/a/t/h'; createdUrl.search = '?query=string'; createUrl.toString(); // Creates https://www.example.com/p/a/t/h?query=string

Instructions

1.

First, let’s import the url module using require() and save it to a const variable called url.

2.

Create a new instance of a URL object using the URL_TO_PARSE variable and assign it to a const variable called myUrl.

3.

Extract the hostname from myUrl and assign it to a const variable called hostname.

4.

Next, extract the path from myUrl and assign it to a const variable called pathname.

5.

Finally, extract the query parameters from myUrl and assign it to a const variable called searchParams.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?