Node.js url.parse(urlString, parseQueryString, slashesDenoteHost) API

Last Updated : 4 Aug, 2026

The url.parse() method in Node.js parses a URL string into a structured object, making it easier to work with individual URL components programmatically.

  • Breaks a URL string into named properties like protocol, host, pathname, query, and hash.
  • Optionally parses the query string into a key-value object instead of a raw string.
  • Handles URLs with leading slashes through the slashesDenoteHost flag.

Syntax

url.parse(urlString, parseQueryString, slashesDenoteHost)

where,

  • urlString: The URL string to be parsed.
  • parseQueryString (optional): A boolean value that determines whether the query string should be parsed into an object.
  • slashesDenoteHost (optional): A boolean value that specifies whether leading slashes indicate the presence of a host.

Return Value: The url.parse() method returns an object with each part of the address as properties.

Note: It throws a TypeError if urlString is not a string and throws a URIError if the auth property exists but is not properly decoded.

Example 1: 

javascript
// Node program to demonstrate the  
// url.parse() method  
    
// Importing the module 'url'
const url = require('url');

// URL address
const address = 'https://www.geeksforgeeks.org/category/projects/';

// Call parse() method using url module
let urlObject = url.parse(address, true);

console.log('URL Object returned after parsing');

// Returns an URL Object
console.log(urlObject)

In this code, url.parse() parses the given URL string and returns an object containing its individual components.

Output: 

Example 2:

javascript
// Node program to demonstrate the  
// url object properties  

// Get different parts of the URL
// using object properties
const url = require('url');

// URL address
const address = 
'https://www.geeksforgeeks.org/category/projects/';

// Call parse() method using url module
let urlObject = url.parse(address, true);

console.log('Url host');

// Returns 'geeksforgeeks.org'
console.log(urlObject.host); 
console.log('Url pathname');

// Returns '/projects'
console.log(urlObject.pathname); 
console.log('Url search');

// Returns '?sort=newest&lang=nodejs'
console.log(urlObject.search); 
 
// Get query data as an object
// Returns an object: 
// { sort: 'newest', lang: 'nodejs' }
let queryData = urlObject.query; 
console.log(queryData);
console.log('Url query object');

// Returns 'nodejs'
console.log(queryData.lang); 

In this code,

  • url.parse() converts the URL into an object containing its components.
  • The host, pathname, and search parts of the URL are extracted.
  • Query parameters are accessed through the query object.

Output: 

Use Cases of url.parse() API

  • Incoming request handling: Extract the pathname and query from a raw request URL in an HTTP server to decide how to respond.
  • Route matching: Use the pathname property to direct traffic to the correct handler in a custom router.
  • Reading query parameters: Access filter, sort, or search values directly from the query object in API endpoints.
  • Pre-processing before redirect: Inspect the protocol or host of a URL before forwarding or redirecting a request.

Reference: https://nodejs.org/docs/latest/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost 

Comment

Explore