How to Get Protocol, Host, and Domain name from URL in Node

In this short guide, we will help you understand how to get a domain name, protocol and hostname from a URL or web link in the Node js application.

To find the utter URL information, we will use the URL() constructor. The URL class gets a newly created URL object in relation to the URL set by the users.

A hostname is a simple string representing the particular authority within the Internet domain. Ideally, hostnames are used to name the web application for addressing intents.

Therefore, to extract the hostname in the node, we will use the as a hostname.

A domain name is an easy-to-remember name that’s associated with a physical address on the Internet. Here is the list of common domain names available.

  • com – commercial business (the most common TLD)
  • org – organizations (typically, nonprofit)
  • gov – government agencies.
  • edu – educational institutions.
  • net – network organizations.
  • mil – military.

Protocols, In general, a set of rules or instructions for transmitting data between electronic devices, such as computers. HTTP, HTTPS are the best examples of protocols.

Node Js Extract Hostname, Protocol, and Domain name from a URL Example

  • Step 1: Make Project Folder
  • Step 2: Run NPM Init
  • Step 3: Build Server File
  • Step 4: Ascertain URL Details
  • Step 5: Display Result in Terminal

Make Project Folder

Open the terminal, type the given command to make the node project directory.

mkdir node-url

Let’s dive into the project folder.

cd node-url

Run NPM Init

You need to run the node package manager’s initializer command to create the package.json file.

npm init

Build Server File

Next, inside your node project, you require to create a app.js file.

Also, you need to add the app file name in the script tag in the package.json file.

We need this configuration because we will execute the command from the terminal to see the result.

  "scripts": {
    "start": "node app.js"

Ascertain URL Details

You have to eventually, head over to app.js file.

In the app file, you have to add the entire given code as mentioned below.

// Pass site url
let siteUrl = ''

let getLinkInfo = new URL(siteUrl)
let host =

// Regex for .com, .io, .net, .org etc
let domainName = host.replace(/^[^.]+\./g, '')

let protocol = getLinkInfo.protocol

console.log('Communication protocol:', protocol)

console.log('Domain name:', domainName)

console.log('Hostname:', host)

Display Result in Terminal

In the final step, we have to get the domain name, get the protocol and extract the hostname.

Therefore, run the suggested command on the terminal.

node app.js

You will get the output based on your url on the console screen.

Communication protocol: https:

Domain name: io



How to Get Protocol, Host, and Domain name from URL in Node

So far in this short yet comprehensive tutorial, we have understood how to pull out the hostname, protocol, and domain name from a string URL in the node js application and that too without using any external dependency.

We believe you have liked this essential guide, have a good day.