0
0
Node.jsframework~15 mins

URL class for parsing in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
URL class for parsing
📖 Scenario: You are building a simple tool to analyze website addresses (URLs). You want to break down a URL into parts like the protocol, hostname, and pathname.
🎯 Goal: Create a Node.js script that uses the built-in URL class to parse a given URL string and extract its components.
📋 What You'll Learn
Create a variable with a URL string
Create a URL object from the string
Extract the protocol, hostname, and pathname from the URL object
Print or store these parts for further use
💡 Why This Matters
🌍 Real World
Parsing URLs is common when working with web addresses, APIs, or routing in web applications.
💼 Career
Understanding how to use the URL class helps in backend and frontend development for handling links, redirects, and data extraction from URLs.
Progress0 / 4 steps
1
Create a URL string variable
Create a variable called myUrl and set it to the string "https://example.com/path/page.html".
Node.js
Need a hint?

Use const to create a variable named myUrl and assign the URL string to it.

2
Create a URL object from the string
Create a variable called parsedUrl and set it to a new URL object created from the myUrl string.
Node.js
Need a hint?

Use new URL(myUrl) to create the URL object and assign it to parsedUrl.

3
Extract protocol, hostname, and pathname
Create three variables: protocol, hostname, and pathname. Assign each the corresponding property from parsedUrl: protocol, hostname, and pathname.
Node.js
Need a hint?

Use dot notation to get protocol, hostname, and pathname from parsedUrl.

4
Log the extracted parts
Add three console.log statements to print protocol, hostname, and pathname each on its own line.
Node.js
Need a hint?

Use console.log() to print each variable on its own line.