Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Pipe

req.hostname

req.hostname returns the request hostname without its port. With proxy trust enabled, Express derives it from X-Forwarded-Host instead of Host.

How it works

req.hostname is a read-only request property containing the hostname extracted from the HTTP Host header. If the app trusts the proxy, Express instead derives it from X-Forwarded-Host; with multiple forwarded values, it selects the first value.

Syntax

req.hostname

Returns

A string containing the request hostname, derived from Host or, when proxy trust is enabled, X-Forwarded-Host; the port is stripped.

Key points

  • The property normally derives its hostname from the incoming HTTP Host header.
  • When proxy trust is enabled, the forwarded hostname may come from either the client or the proxy.
  • The returned hostname omits the port number.

Common mistakes

  • Enabling proxy trust changes the source of the hostname to X-Forwarded-Host, which may be supplied by the client or proxy; configure proxy trust only for infrastructure you control.
  • Multiple X-Forwarded-Host headers do not produce a combined hostname; Express uses the first header.
  • A comma-separated forwarded-host header is treated similarly: only its first value is selected.

Example: Read the request hostname

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

app.get('/', (req, res) => {
  res.json({ hostname: req.hostname });
});

app.listen(3000);

Run the app and request the root path; the response contains the hostname Express derived from the request headers.

When to use it

Build host-aware responses

Generate canonical URLs or host-aware redirects from the incoming request hostname.

Resolve virtual hosts

Select tenant or site configuration based on the requested hostname.

Enrich request logs

Log the hostname associated with an incoming request.

Interview questions

QWhat does req.hostname return?

It is a string property on the Express request object. Normally it contains the hostname from the Host header, with the port excluded.

Follow-up: What changes behind a trusted proxy? Express reads X-Forwarded-Host instead, so proxy trust must match the deployment topology.

QHow does proxy trust affect req.hostname?

When trust proxy does not evaluate to false, Express uses X-Forwarded-Host rather than Host. That header can be set by either the client or the proxy.

Follow-up: How does Express handle multiple forwarded-host values? It uses the first header, and for comma-separated values it uses the first value.

QHow does req.hostname differ from req.host?

req.hostname is the current property for obtaining the hostname. The related req.host property is a deprecated alias and includes the port-stripped host value described by the documentation.

Follow-up: Which property should new code use? Use req.hostname, not the deprecated req.host alias.

  • req.host
  • trust proxy
  • req.protocol
  • req.ip
  • req.ips

Source: Request Object - Express.js 4.x. Checked 2026-10-15.