The URL module in Node.js provides utilities for URL resolution and parsing. In the context of server-side development, especially in applications dealing with HTTP requests, parsing URLs is an essential part of handling routing, query parameters, host identification, and more. This module helps developers break down and manipulate URLs efficiently.
The url module is a core module in Node.js, which means you do not need to install it using npm. It is available out-of-the-box.
const url = require('url');
Alternatively, in modern Node.js (ES6+), you can use:
import { URL } from 'url';
Before diving into parsing, letβs quickly revisit what a URL consists of. A typical URL looks like:
https://username:password@www.example.com:8080/path/name?query=value#hash
This can be broken down into several components:
Node.js provides two main ways of parsing URLs:
This is the legacy method. It returns an object containing properties such as protocol, host, pathname, etc.
const url = require('url');
const parsedUrl = url.parse('https://www.example.com:8080/path/name?query=value#section');
console.log(parsedUrl);
Output:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.example.com:8080',
port: '8080',
hostname: 'www.example.com',
hash: '#section',
search: '?query=value',
query: 'query=value',
pathname: '/path/name',
path: '/path/name?query=value',
href: 'https://www.example.com:8080/path/name?query=value#section'
}
Introduced in Node.js v7 and above, this API is compliant with modern standards. It's now the preferred method.
const { URL } = require('url');
const myURL = new URL('https://www.example.com:8080/path/name?query=value#section');
console.log(myURL);
Output:
URL {
href: 'https://www.example.com:8080/path/name?query=value#section',
origin: 'https://www.example.com:8080',
protocol: 'https:',
username: '',
password: '',
host: 'www.example.com:8080',
hostname: 'www.example.com',
port: '8080',
pathname: '/path/name',
search: '?query=value',
searchParams: URLSearchParams { 'query' => 'value' },
hash: '#section'
}
The full URL string.
Returns the protocol and host.
The protocol used (e.g., http:, https:).
Authentication credentials, if included.
Combines hostname and port.
Only the domain name or IP address.
The port number used by the server.
The path part of the URL after the host.
The entire query string, including the leading question mark.
An instance of URLSearchParams to access individual query parameters.
The fragment identifier including the hash symbol.
The searchParams property gives powerful access to query parameters in the URL.
const { URL } = require('url');
const myURL = new URL('https://example.com/path?name=John&age=25');
console.log(myURL.searchParams.get('name')); // John
console.log(myURL.searchParams.has('age')); // true
console.log(myURL.searchParams.getAll('name')); // ['John']
You can also manipulate the parameters:
myURL.searchParams.append('city', 'Chennai');
myURL.searchParams.set('age', '30');
myURL.searchParams.delete('name');
console.log(myURL.toString());
// Output: https://example.com/path?age=30&city=Chennai
Relative URLs are resolved with respect to a base URL:
const { URL } = require('url');
const base = new URL('https://example.com/path/');
const relative = new URL('subpage.html', base);
console.log(relative.href);
// Output: https://example.com/path/subpage.html
You can build URLs dynamically by creating a new URL object and setting its properties.
const { URL } = require('url');
const newURL = new URL('https://example.com');
newURL.pathname = '/user/profile';
newURL.searchParams.append('id', '100');
newURL.hash = '#section2';
console.log(newURL.href);
// Output: https://example.com/user/profile?id=100#section2
Parsing URLs is vital in building a server that handles different routes and query parameters.
const http = require('http');
const { URL } = require('url');
const server = http.createServer((req, res) => {
const myURL = new URL(req.url, `http://${req.headers.host}`);
const path = myURL.pathname;
const query = myURL.searchParams;
if (path === '/hello') {
const name = query.get('name') || 'Guest';
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Hello, ${name}`);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Try accessing http://localhost:3000/hello?name=Lakshmi in your browser.
Sometimes, URLs contain special characters that need to be encoded or decoded.
const name = 'John Doe';
const encoded = encodeURIComponent(name);
console.log(encoded); // John%20Doe
const decoded = decodeURIComponent(encoded);
console.log(decoded); // John Doe
Though url.parse() and its siblings (like url.format() and url.resolve()) are still functional, they are considered legacy. Use the WHATWG URL API for new projects to ensure future compatibility and better standards support.
| Feature | url.parse() | WHATWG URL API |
|---|---|---|
| Standards Compliance | No | Yes |
| Query Parameters Access | Plain string | URLSearchParams |
| Preferred for Modern Code | No | Yes |
The URL module in Node.js is a powerful tool for parsing, constructing, and manipulating URLs. The introduction of the WHATWG-compliant URL class has modernized the approach, making it easier and more intuitive to work with URLs. Understanding how to dissect URLs, manage query strings, and dynamically build URLs is critical for any backend developer working with Node.js.
Whether you're handling HTTP requests, interacting with external APIs, or simply validating links, mastering the URL module gives you a solid foundation to work with URLs efficiently and securely in your applications.
A function passed as an argument and executed later.
Runs multiple instances to utilize multi-core systems.
Reusable blocks of code, exported and imported using require() or import.
nextTick() executes before setImmediate() in the event loop.
Starts a server and listens on specified port.
Node Package Manager β installs, manages, and shares JavaScript packages.
A minimal and flexible web application framework for Node.js.
A stream handles reading or writing data continuously.
It processes asynchronous callbacks and non-blocking I/O operations efficiently.
Node.js is a JavaScript runtime built on Chrome's V8 engine for server-side scripting.
An object representing the eventual completion or failure of an asynchronous operation.
require is CommonJS; import is ES6 syntax (requires transpilation or newer versions).
Use module.exports or exports.functionName.
Variables stored outside the code for configuration, accessed using process.env.
MongoDB, often used with Mongoose for schema management.
Describes project details and manages dependencies and scripts.
Synchronous blocks execution; asynchronous runs in background without blocking.
Allows or restricts resources shared between different origins.
Use try-catch, error events, or middleware for error handling.
Provides file system-related operations like read, write, delete.
Using event-driven architecture and non-blocking I/O.
Functions in Express that execute during request-response cycle.
A set of routes or endpoints to interact with server logic or databases.
Yes, it's single-threaded but handles concurrency using the event loop and asynchronous callbacks.
Middleware to parse incoming request bodies, like JSON or form data.
Copyrights © 2024 letsupdateskills All rights reserved