URL.port API in Node.js
×


URL.port API in Node.js

200

Introduction to URL.port API in Node.js

In Node.js, the built-in URL class provides a convenient way to parse, access, and manipulate components of a URL string. One such property is URL.port, which allows developers to retrieve or modify the port number specified in a URL. This is particularly useful when working with URLs pointing to custom ports or when dynamically building network-based applications.

What is URL.port?

The URL.port property returns the port number of the URL as a string. If the port is not explicitly mentioned in the URL, it returns an empty string. You can also use this property to assign a new port to the URL by setting it directly.

Syntax


// Get the port
url.port

// Set a new port
url.port = '3000'
    

Example: Getting the Port

The following example demonstrates how to access the port from a given URL:


const { URL } = require('url');

const myURL = new URL('http://localhost:8080/path');
console.log(myURL.port); // Output: 8080
    

Here, the port number 8080 is explicitly included in the URL, and accessing the port property returns it as a string.

Example: Setting the Port

You can easily change the port of a URL by assigning a new value to url.port:


const { URL } = require('url');

const myURL = new URL('https://example.com');
myURL.port = '3000';

console.log(myURL.href); // Output: https://example.com:3000/
    

In this case, the URL is updated to include the new port number 3000.

Default Ports and Empty Strings

If the port is not specified in the URL and the scheme uses a default port (like 80 for HTTP or 443 for HTTPS), then url.port will return an empty string:


const myURL = new URL('https://google.com');
console.log(myURL.port); // Output: (empty string)
    

This behavior helps differentiate between explicitly set ports and default ones.

Why Use URL.port?

Using the URL.port API is beneficial in several situations:

  • Port validation: Check if a URL is pointing to a specific port.
  • Dynamic URL generation: Create links or endpoints based on runtime port numbers.
  • Proxy configuration: Identify and update port settings in network applications.

Key Notes

  • The port is returned as a string, even if it's a number like 8080.
  • Only valid port numbers (from 0 to 65535) are accepted. Invalid assignments will throw an error.
  • Modifying the port affects the full URL structure accessed via url.href.

Conclusion

The URL.port property in Node.js offers a straightforward way to work with port numbers in URLs. Whether you're building server-side applications, working with custom ports, or adjusting network configurations, this property provides a clean and reliable interface for managing the port component of a URL.



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!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat