Express.js req.subdomains Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

req.subdomains is a string array of subdomain labels from the request hostname. Express takes req.hostname, splits on dots, and drops the trailing “main domain” labels controlled by subdomain offset (default 2).

Official example: Host tobi.ferrets.example.com → ['ferrets', 'tobi']. Index 0 is closest to the apex domain. Useful for multi-tenant apps (acme.example.com → tenant acme).

Syntax

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

Express
req.subdomains
app.set('subdomain offset', 2)   // default
app.set('subdomain offset', 3)   // e.g. example.co.uk
CallWhat it does
req.subdomainsPrimary signature.
app.set('subdomain offset', 2) // defaultRelated call form.
app.set('subdomain offset', 3) // e.g. example.co.ukRelated call form.

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.subdomains
app.set('subdomain offset', 2)   // default
app.set('subdomain offset', 3)   // e.g. example.co.uk

Parameters

This Express.js property is read from the request or response — it has no call parameters.

Return Value

req.subdomains is a read-only array of subdomain labels from the hostname. It returns [] when there is no hostname or nothing left after the subdomain offset.

Express
// Host: tobi.ferrets.example.com
req.subdomains; // ['ferrets', 'tobi']

Express.js Examples

Example 1 — Read the Subdomain Array

Return whatever Express parses from the Host header (default offset 2).

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

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

app.listen(3000);

How It Works

  1. Hostname first. req.hostname is the host without the port.
  2. Default offset 2. example.com is treated as the main domain.
  3. Remaining labels. ferrets and tobi become the subdomain array.
  4. Order. Closest-to-apex comes first: subdomains[0] === 'ferrets'.

Example 2 — Adjust subdomain offset

Raise the offset when the public suffix has three labels (for example co.uk).

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

app.set('subdomain offset', 3);

app.get('/who', (req, res) => {
  res.json({
    offset: 3,
    subdomains: req.subdomains,
    first: req.subdomains[0] || null
  });
});

app.listen(3000);

How It Works

  1. Set once at boot. app.set('subdomain offset', 3) applies to every request.
  2. Skip three labels. For app.example.co.uk, uk, co, and example are the “domain”.
  3. Left-overs are subdomains. Only app remains.
  4. Wrong offset hurts. Offset 2 on co.uk would incorrectly treat example as a subdomain.

Example 3 — Resolve a Tenant from the Subdomain

Simple multi-tenant gate using the first subdomain label.

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

const TENANTS = new Set(['acme', 'globex']);

app.use((req, res, next) => {
  const tenant = req.subdomains[0];
  if (!tenant || !TENANTS.has(tenant)) {
    return res.status(404).send('Unknown tenant');
  }
  req.tenant = tenant;
  next();
});

app.get('/', (req, res) => {
  res.send('Hello, ' + req.tenant);
});

app.listen(3000);

How It Works

  1. Take the first label. With default offset, acme.example.com → ['acme'].
  2. Allowlist tenants. Only known names continue; others get 404.
  3. Attach context. req.tenant is available to later handlers.
  4. Apex has no tenant. Bare example.com yields an empty array — handle that path separately if needed.

Runtime Support

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

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

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It is a String array of subdomain labels from the request hostname. For Host tobi.ferrets.example.com with the default offset, it is ['ferrets', 'tobi'].
An app setting (default 2) that tells Express how many trailing host labels are the “main domain” (for example example.com). Labels before that are returned as subdomains.
Use app.set('subdomain offset', n). Use 3 for domains like example.co.uk where the public suffix has three labels.
Indirectly. req.subdomains is derived from req.hostname, which can use X-Forwarded-Host when trust proxy is enabled.
No. It is a server-side Express property. Browsers only send the Host (or forwarded host) header.

Next: req.xhr

See when Express considers a request an XHR / AJAX call.

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