Node Show Domain, Host, Protocol Names from URL Tutorial

Last updated on: by Digamber

In this profound tutorial, we will learn how to use a site URL eloquently and, more than that, how to quickly get a domain name, protocol name, and hostname from a given site URL or link in the node js application.

A URL is an internet address ideally known as a web address, and it is a simple string that points towards a specific location on the internet.

In general, a URL consists of multiple components, a protocol, domain name, and hostname.

We will not be using any third-party library to extract the host, domain, and protocol name; instead, we will use the new Url() constructor class.

How to Obtain Hostname, Protocol, Domain name from URL in Node

  • Step 1: Build Node Project
  • Step 2: Create App File
  • Step 3: Get URL Information
  • Step 4: Run Node Application

Build Node Project

In the first step, we will build the new node project.

The first step is to create a blank new folder, where we will keep all the project related files and folders.

mkdir node-vlog

Type the cd command followed by project name to enter into the project.

cd node-vlog

In the next step, we will run the npm command and generate the package.json file, this file holds the project related information.

npm init

Create App File

In the next step, you have to make the server.js file; in this file, we will be writing the code to build the feature.

In the next step, we will register the script server.js name within the scripts array.

Therefore, open the package.json file and keep the given code as it is.

...
...
  "scripts": {
    "start": "node server.js"
  },
...
...

Get URL Information

To get the URL details, you have to move to the server.js file. After that, define the site URL; you may pass any site URL like we used our site URL.

Then create the siteDetails instance from the new URL() constructor; afterward, you can access the host and protocol methods to extract the domain, host, and protocol details.

const resourceURL = 'https://positronx.io/'
const siteDetails = new URL(resourceURL)

const host = siteDetails.host
console.log('Host name:', host)

const regexDomain = host.replace(/^[^.]+\./g, '')
console.log('Domain name:', regexDomain)

const protocol = siteDetails.protocol
console.log('Protocol name:', protocol)

Run Node Application

In the final step, just again open the terminal, type the command and hit enter.

node server.js

Once the node server is invoked thereafter you can see the site details on the console screen.

Host name: positronx.io
Domain name: io
Protocol name: https:

Conclusion

Node Show Domain, Host, Protocol Names from URL Tutorial

A constructor class is a core fundamental of JavaScript; It contains various methods and properties that help solve the specific problem programmatically.

Today, in this guide, we shed light on using the new Url() method to pull out the protocol name, domain name, and hostname in the node js application.

We have also learned how to use regular expressions to filter out the various domain names such as .io, .net, .com, .org, etc., in the node js environment.

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.