Sling Academy
Home/Node.js/Node.js: Get domain, hostname, and protocol from a URL

Node.js: Get domain, hostname, and protocol from a URL

Last updated: December 02, 2023

By using the URL class, a built-in module of Node.js, we can easily extract domain/hostname and protocol from a given url without using any third-party package.

Domain, hostname, and protocol

A domain name is the address of a website that people type in the browser URL bar to visit your website. For example, slingacademy.com is a domain name.

A hostname is a domain name assigned to a host computer. This is usually a combination of the host’s local name with its parent domain’s name. For example, www.slingacademy.com, api.slingacademy.com consists of local hostnames (www and api), and the domain slingacademy.com.

An internet protocol is a defined set of rules and regulations that determine how data is transmitted in telecommunications and computer networking. For instance, WebSocket, HTTP, and HTTPS are protocols.

Node.js URL API

The URL class was first added in Node.js 6.13. From Node.js 10.0 and above, the class is available on the global object so you can use it without this line:

const URL = require('url'); 
// This is no longer necessary for Node 10.0+

Create a URL object from a string URL:

const urlObject = new URL('a-string-URL-here');

The URL object comes with some properties including hostname, protocol, port, pathname, hash, host, href, origin, password, pathname, search, searchParams, and username.

In some cases, the domain name may be different from the hostname and we need to use a regular expression to get the correct domain name (you can see this in the example below).

Example

The code:

// Replace this with your own url
const myUrl =
    'https://www.slingacademy.com/article/how-to-set-up-typescript-for-a-node-js-project/';


const urlObject = new URL(myUrl);
const hostName = urlObject.hostname;

// The regular expression below works with .dev, .com, .net, .org and other top level domain names
let domainName = hostName.replace(/^[^.]+\./g, '');

const protocol = urlObject.protocol;

console.log('Hostname:', hostName);
console.log('Domain:', domainName);
console.log('Protocol:', protocol);

Output:

Hostname: www.slingacademy.com
Domain: slingacademy.com
Protocol: https:

That’s it. Happy coding!

Next Article: How to Set Custom Response Headers in Express.js

Previous Article: How to Set Default Time Zone in Node.js

Series: The First Steps to Node.js

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates