Express.js: Parsing Predomain from URL works with localhost but not with

I’ve implemented a simple Express.js application that extracts the predomain from incoming requests’ hostnames. The code seems to be functioning correctly when I use localhost:3000, but it fails when I try

Here’s a snippet of the code I’m using:

const express = require('express');
const app = express();

app.use((req, res, next) => {
  const parts = req.hostname.split('.');
  req.predomain = parts[0];

app.get("", (req, res) => {
  res.json({ predomain: req.predomain });

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);

When I access http://harsh.localhost:3000, the API correctly returns the predomain as expected (harsh). However, when I access http://harsh. the postman or chrome cannot find the address itself. I’m curious as to why this might be happening and how I can modify my code to correctly handle the extraction of the predomain even when using or something else.

  • 3

    A IP Address ( is not the same as a Domain name ( / A IP Address can not have/be treated the same as a domain/sub domain.


The issue here is conceptual, not with your code – unlike a domain that may have subdomains, an IP (v4) address is always four integers delimited by dots. You simply can’t add a subdomain to an IP address.

Leave a Comment