URL.host API in Node.js
0 208
Introduction to URL.host API in Node.js
When working with URLs in Node.js, you often need to extract specific parts of a web address. One such important property is host
, which represents the hostname along with the port number (if specified). Node.js provides an easy way to interact with URLs using the URL
class, and the URL.host
property is especially useful for parsing or modifying this portion of the URL.
What is URL.host?
The URL.host
property returns or sets the host of the URL, which is a combination of the hostname and the port (if included). For example, in the URL http://localhost:3000
, the host would be localhost:3000
. If the port is not explicitly defined, it simply returns the hostname.
This property is part of the url
module in Node.js, which makes working with different components of a URL more structured and convenient.
Syntax of URL.host
// To get the host
url.host
// To set a new host
url.host = 'newhost:port'
Example: Accessing the Host
Let’s see how to get the host from a given URL:
const { URL } = require('url');
const myURL = new URL('http://example.com:8080/path');
console.log(myURL.host); // Output: example.com:8080
In this example, the host
property correctly returns the hostname and port together.
Example: Modifying the Host
You can also change the host value dynamically:
const { URL } = require('url');
const myURL = new URL('https://example.com');
myURL.host = 'localhost:3000';
console.log(myURL.href); // Output: https://localhost:3000/
Here, the host
is updated to localhost:3000
, and this change reflects in the full URL.
Difference Between host and hostname
It’s important not to confuse host
with hostname
:
- host: Includes both hostname and port (e.g.,
example.com:8080
). - hostname: Returns only the domain or IP part without the port (e.g.,
example.com
).
Use Cases of URL.host
The URL.host
property is helpful in many real-world scenarios, such as:
- Logging or monitoring incoming requests and extracting host details.
- Modifying API endpoints dynamically in development or production environments.
- URL validation or parsing in web applications and tools.
Conclusion
The URL.host
API in Node.js is a simple yet powerful property that makes it easy to work with the hostname and port in a URL. Whether you’re analyzing request URLs, rewriting paths, or modifying server details, this feature is a reliable tool in your Node.js development toolkit. Understanding how to use it effectively helps you handle URLs in a more structured and error-free way.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments