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
Call
What it does
req.hostname
Primary signature.
Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.
Internal Host may be private. The app often sees only the proxy’s address.
Trust one hop.trust proxy tells Express to prefer forwarded headers from that proxy.
First forwarded host wins. Multiple values → Express uses the first.
Harden the edge. Your proxy should overwrite client-supplied X-Forwarded-Host.
Compatibility
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.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
req.hostnameExpress 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.