Express.js req.host Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

req.host is the host derived from the Host HTTP header. In Express 5 it keeps the port when present — for example Host: example.com:3000 yields example.com:3000.

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

Syntax

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

Express
req.host
CallWhat it does
req.hostPrimary 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.host

Parameters

ParameterDescription
Host headerOptional. Browsers send it on every HTTP/1.1 request
trust proxyOptional. Switches source to X-Forwarded-Host
Allowlist / validationOptional. Stops open-redirect style host tricks

Return Value

req.host is a string property derived from the Host header (and trust-proxy rules). On Express 5 it can include the port when present.

Express
req.host;     // e.g. 'example.com' or 'example.com:3000' (Express 5)
req.hostname; // host name without port

Express.js Examples

Example 1 — Read req.host (Official Pattern)

Log the host Express derives from the Host header, including the port.

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

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

app.listen(3000);

How It Works

  1. Client sends Host. HTTP/1.1 requests include the authority they think they reached.
  2. Express derives req.host. In Express 5 the port stays on the string.
  3. No parsing needed. You do not call a method — read the property.
  4. Return it for learning. Production apps usually validate before using it in links.

Example 2 — Compare host and hostname

See the port difference side by side on one request.

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

app.get('/where-am-i', (req, res) => {
  res.json({
    host: req.host,           // may include :port
    hostname: req.hostname,   // name only
    protocol: req.protocol,
    absolute: req.protocol + '://' + req.host + req.originalUrl
  });
});

app.listen(3000);

How It Works

  1. Same request, two views. host keeps :3000; hostname drops it.
  2. Add protocol. req.protocol is http or https.
  3. Build an absolute URL. Concatenate protocol, host, and req.originalUrl.
  4. Pick the right field. Need the port in links → req.host. Name only → req.hostname.

Example 3 — trust proxy and X-Forwarded-Host

Behind a reverse proxy, enable trust so Express reads the public host the proxy forwards.

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

// Trust one hop (your reverse proxy). Prefer IP/subnet in production.
app.set('trust proxy', 1);

app.get('/public-host', (req, res) => {
  // With trust proxy on:
  // X-Forwarded-Host: app.example.com  →  req.host is that value
  res.json({
    host: req.host,
    hostname: req.hostname
  });
});

app.listen(3000);

How It Works

  1. Proxy terminates TLS / public DNS. The app may only see an internal Host.
  2. Enable trust carefully. trust proxy tells Express to use forwarded headers from that hop.
  3. First forwarded host wins. If several values are listed, Express uses the first.
  4. Harden the proxy. Your edge proxy should overwrite spoofed X-Forwarded-* headers from clients.

Runtime Support

req.host 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.host

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.host Express on Node.js

Bottom line: On Express 5, req.host keeps the port. Use req.hostname when you only need the name.

Frequently Asked Questions

It is the host derived from the Host header (or X-Forwarded-Host when trust proxy is enabled). In Express 5 it keeps the port when present, for example example.com:3000.
req.host can include the port. req.hostname is the hostname only — Host example.com:3000 yields hostname example.com.
When trust proxy is not false, Express reads X-Forwarded-Host instead of Host. Use that only behind a proxy you trust.
Express 4 incorrectly stripped the port from req.host. Express 5 keeps the port. Prefer Express 5 behavior when you need host:port.
Not blindly. Host and X-Forwarded-Host can be spoofed. Validate against an allowlist when building absolute URLs or tenant routing.

Next: req.hostname

Get the hostname without the port from the same Host / forwarded-host source.

req.hostname tutorial →

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