KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

5 Questions

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

5 Questions

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

5 Questions

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

5 Questions

MySQL Order By

10 min

Recap Quiz

5 Questions

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

5 Questions

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

5 Questions

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

Node.jsNode.js Tutorial
Lesson

Node.js URL Module

10 min reading
Free Course

Node.js URL Module: Parsing & Constructing Web Request URLs

The node:url module provides utilities for URL resolution, parsing, serialization, and search query string manipulation adhering to the modern standard WHATWG URL API specification.

WHATWG URL Component Breakdown

flowchart LR
    A["https://user:[email protected]:8080/v1/users?role=admin#details"]
    A --> B["protocol: 'https:'"]
    A --> C["hostname: 'api.example.com'"]
    A --> D["port: '8080'"]
    A --> E["pathname: '/v1/users'"]
    A --> F["searchParams: { role: 'admin' }"]
    A --> G["hash: '#details'"]

Practical Code Example

import { URL, URLSearchParams } from 'node:url';

// 1. Parsing full URL string
const requestUrl = 'https://store.example.com:8443/products/search?category=laptops&sort=price_asc#results';
const parsedUrl = new URL(requestUrl);

console.log(`Protocol: ${parsedUrl.protocol}`); // 'https:'
console.log(`Host: ${parsedUrl.host}`);         // 'store.example.com:8443'
console.log(`Pathname: ${parsedUrl.pathname}`); // '/products/search'
console.log(`Port: ${parsedUrl.port}`);         // '8443'

// 2. Manipulating Search Parameters (Query String)
const params = parsedUrl.searchParams;
console.log(`Category Filter: ${params.get('category')}`); // 'laptops'

// Add dynamic query parameters
params.append('inStock', 'true');
params.set('page', '2'); // update or insert

console.log(`Updated Query String: ${parsedUrl.search}`); 
// '?category=laptops&sort=price_asc&inStock=true&page=2'

// 3. Constructing URLs programmatically
const baseApiUrl = new URL('/api/v2/metrics', 'https://dash.analytics.io');
baseApiUrl.searchParams.set('range', '7d');

console.log(`Constructed Target URL: ${baseApiUrl.href}`);
// 'https://dash.analytics.io/api/v2/metrics?range=7d'

Best Practices & Gotchas

  • Use standard URL global: The legacy url.parse() method is deprecated in modern Node.js environments; use the WHATWG new URL() constructor instead.
  • Sanitize Search Parameters: Always check and sanitize input parameters retrieved via url.searchParams.get() before passing them into SQL or file queries.
  • Relative Path Resolution: Pass a valid base string (e.g. http://localhost) when parsing relative incoming request paths (like req.url).

Self-Check Challenge

Parse the string 'https://api.github.com/repos/nodejs/node?per_page=100' and extract the repository name path and the per_page param value.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum