URL.origin API in Node.js
×


URL.origin API in Node.js

200

Introduction to URL.origin API in Node.js

Node.js provides a powerful URL class that allows developers to work easily with different parts of a URL. One of the helpful properties available is URL.origin. This property returns the origin of a URL, which includes the protocol, hostname, and port number (if it's specified). Understanding how to use origin is especially important when validating or comparing URLs.

What is URL.origin?

The URL.origin property returns a string that represents the origin of the URL. The origin consists of:

  • Protocol (e.g., http: or https:)
  • Hostname (e.g., example.com)
  • Port (if explicitly specified, e.g., :3000)

This property is read-only, meaning you can use it to retrieve the origin but not to set or modify it directly.

Syntax of URL.origin

url.origin

Example: Using URL.origin

Let’s look at a basic example of how URL.origin works in practice:


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

const myURL = new URL('https://example.com:8080/path/page');
console.log(myURL.origin); // Output: https://example.com:8080
    

In this example, the origin returns the scheme, hostname, and port (since it's explicitly provided).

Origin Without a Port

If no port is included in the URL, the origin still forms correctly with just the protocol and hostname:


const myURL = new URL('https://www.google.com/search');
console.log(myURL.origin); // Output: https://www.google.com
    

The result omits the port because it's not specified. The default port for HTTPS (443) is implied.

Why Use URL.origin?

Using URL.origin can be useful in a variety of use cases:

  • Security validation: Ensuring that a URL belongs to a trusted origin before proceeding with requests.
  • Cross-origin checks: In server-side rendering or API integrations, validating if two URLs share the same origin.
  • Logging and monitoring: Quickly extracting and storing the base origin of incoming URLs.

Things to Keep in Mind

  • origin is a read-only property—you can't assign a value to it directly.
  • The port is only included in the output if it was explicitly provided in the URL.
  • Works only for URLs with a valid scheme like http, https, or ftp.

Conclusion

The URL.origin API in Node.js makes it easy to retrieve the base origin of any URL. Whether you're working on authentication, redirect handling, or simply need to extract the root address, this property is a reliable tool in your development toolbox. It offers a clean and consistent way to access protocol, hostname, and port in one string.



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