Express.js req.hostname Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.hostname is the hostname derived from the Host HTTP header — without the port. Official example: Host: example.com:3000 → req.hostname is example.com.

When trust proxy is enabled, Express may read X-Forwarded-Host instead (first value if several are listed). Subdomains stay on the string (api.example.com). This is a server-side property on the request object.

Syntax

Read req.hostname in your Express.js route handler. Start with this line.

Express
req.hostname
CallWhat it does
req.hostnamePrimary signature.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

req.hostname

Parameters

ParameterDescription
Host headerOptional. Browsers send it on every HTTP/1.1 request
trust proxyOptional. Switches source to X-Forwarded-Host
AllowlistOptional. Stops spoofed hostnames from selecting the wrong tenant

Return Value

req.hostname is a string property: the hostname without the port (for example api.example.com).

Express
// Host: example.com:3000
req.hostname; // 'example.com'
req.host;     // may include port on Express 5

Express.js Examples

Example 1 — Read Hostname (Official Pattern)

Port is stripped from the Host header — same result as the official docs.

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

app.get('/', (req, res) => {
  // Host: example.com:3000  →  'example.com'
  console.log(req.hostname);
  res.json({
    hostname: req.hostname,
    host: req.host
  });
});

app.listen(3000);

How It Works

  1. Client sends Host. Often includes a non-default port in local or custom setups.
  2. Express strips the port. req.hostname keeps only the name.
  3. Compare with req.host. On Express 5, host still shows :3000.
  4. Use the right field. Domain logic → hostname; full authority with port → host.

Example 2 — Allowlisted Hostname Routing

Route by hostname only after checking an allowlist — never trust a raw Host alone.

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

const ALLOWED = new Set(['app.example.com', 'admin.example.com']);

app.get('/', (req, res) => {
  const name = req.hostname;

  if (!ALLOWED.has(name)) {
    return res.status(400).send('Unknown host');
  }

  res.json({
    hostname: name,
    area: name.startsWith('admin.') ? 'admin' : 'app'
  });
});

app.listen(3000);

How It Works

  1. Read the hostname. Port noise is already gone, so set membership is simple.
  2. Allowlist first. Reject anything not explicitly permitted.
  3. Branch on the name. Here admin.* vs app.* picks an area label.
  4. Still not encryption. DNS and TLS must also match your real domains in production.

Example 3 — trust proxy and Forwarded Host

Behind a reverse proxy, trust that hop so Express uses the public hostname.

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

app.set('trust proxy', 1);

app.get('/public-name', (req, res) => {
  // X-Forwarded-Host: app.example.com
  // → req.hostname === 'app.example.com'
  res.json({ hostname: req.hostname });
});

app.listen(3000);

How It Works

  1. Internal Host may be private. The app often sees only the proxy’s address.
  2. Trust one hop. trust proxy tells Express to prefer forwarded headers from that proxy.
  3. First forwarded host wins. Multiple values → Express uses the first.
  4. Harden the edge. Your proxy should overwrite client-supplied X-Forwarded-Host.

Runtime Support

req.hostname is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send the Host header.

✓ Server · Express 4 & 5

req.hostname

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.hostname Express on Node.js

Bottom line: Use req.hostname for name-only checks. Pair with an allowlist whenever the value affects security or tenancy.

Frequently Asked Questions

It is the hostname derived from the Host header (or X-Forwarded-Host when trust proxy is enabled), without the port. Host example.com:3000 yields example.com.
req.hostname is name-only. req.host can include the port in Express 5 (example.com:3000).
Yes. api.example.com stays api.example.com. Use req.subdomains when you need the subdomain segments as an array.
When trust proxy is not false, Express uses X-Forwarded-Host (first value if several). Configure that only behind a proxy you trust.
It is a good starting point, but always validate against an allowlist — clients can spoof Host / forwarded-host headers.

Next: req.ip

Read the client IP address (and how trust proxy changes it).

req.ip tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful