0
0
Node.jsframework~10 mins

URL class for parsing in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - URL class for parsing
Create URL instance with string
Parse string into parts
Protocol
Access parts via properties
Use parts in code or output
The URL class takes a web address string, breaks it into parts like protocol and hostname, and lets you access each part easily.
Execution Sample
Node.js
const url = new URL('https://example.com:8080/path?query=123');
console.log(url.protocol);
console.log(url.hostname);
console.log(url.port);
console.log(url.pathname);
console.log(url.search);
This code creates a URL object and prints its parts: protocol, hostname, port, path, and query string.
Execution Table
StepActionInput/StateResult/Output
1Create URL instance'https://example.com:8080/path?query=123'URL object created with full string
2Parse protocolURL string'https:'
3Parse hostnameURL string'example.com'
4Parse portURL string'8080'
5Parse pathnameURL string'/path'
6Parse search paramsURL string'?query=123'
7Access url.protocolURL object'https:'
8Access url.hostnameURL object'example.com'
9Access url.portURL object'8080'
10Access url.pathnameURL object'/path'
11Access url.searchURL object'?query=123'
12Console outputAccessed partsPrint each part to console
💡 All parts parsed and accessed; execution ends after printing.
Variable Tracker
VariableStartAfter Step 1After Step 6Final
urlundefinedURL object with full stringURL object with parsed partsURL object with accessible properties
Key Moments - 3 Insights
Why does url.protocol include the colon ':' at the end?
The URL class includes the colon as part of the protocol property by design, so 'https:' includes ':' to clearly mark the scheme. See execution_table rows 2 and 7.
What happens if the URL string does not include a port?
The port property will be an empty string ''. This is because the URL class parses only what is present. See execution_table row 4 for port parsing.
Can we change parts like hostname after creating the URL object?
Yes, the URL object properties are writable. Changing them updates the full URL string. This is not shown here but is part of the URL class behavior.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of url.pathname at step 10?
A'/path'
B'path'
C'/path?query=123'
D'' (empty string)
💡 Hint
Check the 'Action' and 'Result/Output' columns at step 10 for pathname value.
At which step does the URL class parse the search parameters?
AStep 4
BStep 9
CStep 6
DStep 11
💡 Hint
Look for 'Parse search params' in the 'Action' column.
If the URL string was 'http://localhost', what would url.port be after parsing?
A'443'
B'' (empty string)
C'80'
D'localhost'
💡 Hint
Refer to key_moments about port property when no port is specified.
Concept Snapshot
URL class parses a web address string into parts.
Use: const url = new URL('https://example.com:8080/path?query=123');
Access parts: url.protocol, url.hostname, url.port, url.pathname, url.search
Protocol includes colon ':'
Port is empty string if missing
Properties are writable to update URL
Full Transcript
The URL class in Node.js takes a web address string and breaks it into parts like protocol, hostname, port, pathname, and search parameters. When you create a new URL object with a string, it parses these parts automatically. You can then access each part using properties like url.protocol or url.hostname. For example, the protocol property includes the colon at the end, such as 'https:'. If the URL does not specify a port, the port property will be an empty string. These properties can also be changed to update the URL. This visual trace shows each step of parsing and accessing these parts.