Vaidikalaya

URL Module In Node


The URL module in Node.js is a core module that provides utilities for URL resolution, parsing, and formatting. It is particularly useful when working with URLs in a web-related context, such as handling HTTP requests and manipulating URL strings.

There are two ways to use the URL module in Nodejs

⮚ Using the URL Class (Modern Approach)

⮚ Using the URL Module (Legacy Approach)


Using the URL Class (Modern Approach)

The URL class is the modern and recommended way to work with URLs in Node.js. It provides a more consistent and standard-compliant API for parsing, formatting, and manipulating URLs. This class is globally available, so you don't need to import any module to use it.

Syntex

const myUrl = new URL();
Example
E:\node-tutorial\app.js
let testURL='http://localhost:3000/users?id=10';

const myUrl = new URL(testURL);

console.log(myUrl);

/*OUTPUT
    URL {
    href: 'http://localhost:3000/users?id=10',    
    origin: 'http://localhost:3000',
    protocol: 'http:',
    username: '',
    password: '',
    host: 'localhost:3000',
    hostname: 'localhost',
    port: '3000',
    pathname: '/users',
    search: '?id=10',
    searchParams: URLSearchParams { 'id' => '10' },
    hash: ''
    }
*/

Once you have a URL object, you can easily access its various components:

E:\node-tutorial\app.js
let testURL='http://localhost:3000/users?id=10';

const myUrl = new URL(testURL);

console.log(myUrl.href);          // http://localhost:3000/users?id=10
console.log(myUrl.protocol);      // http:
console.log(myUrl.hostname);      // localhost
console.log(myUrl.port);          // 3000
console.log(myUrl.pathname);      // /users
console.log(myUrl.search);        // ?id=10

/*OUTPUT
    http://localhost:3000/users?id=10
    http:    
    localhost
    3000    
    /users  
    ?id=10  
*/

Getting query parameters

E:\node-tutorial\app.js
let testURL='http://localhost:3000/users?id=10';

const myUrl = new URL(testURL);

let id=myUrl.searchParams.get('id');

console.log(id)

/*OUTPUT
    10
*/

Using the url Module (Legacy Approach)

Before the URL class was introduced, the url module was used for parsing and formatting URLs. This module provides two key functions: 'url.parse()' and 'url.format()'However, 'url.parse()' is now deprecated, and it's recommended to use the URL class instead.

For the legacy approach in Node.js, it is mandatory to import the URL module using require.

const url=require('url')

Example

javascript
const url=require('url')

let testURL='http://localhost:3000/users?id=10';

const myUrl = url.parse(testURL);

console.log(myUrl);

/*OUTPUT
    Url {
    protocol: 'http:',    
    slashes: true,
    auth: null,
    host: 'localhost:3000',
    port: '3000',
    hostname: 'localhost',
    hash: null,
    search: '?id=10',
    query: 'id=10',
    pathname: '/users',
    path: '/users?id=10',
    href: 'http://localhost:3000/users?id=10'
    }
*/

Moving forward, it's best to use the URL class for parsing and handling URLs in Node.js, as it is more robust and is the recommended approach in modern Node.js applications.