Express req.subdomains Property
What you’ll learn
- How to read and interpret
req.subdomains. - How
subdomain offsetchanges parsing. - How to route traffic by tenant subdomain.
- How proxy and host settings impact subdomain detection.
Usage syntax
javascript
req.subdomains
app.set('subdomain offset', 2)1
Read subdomain array from host
javascript
app.get('/tenant', function (req, res) {
res.json({ subdomains: req.subdomains });
});2
Adjust subdomain offset for custom domains
javascript
app.set('subdomain offset', 3);
app.get('/who', function (req, res) {
res.send('Tenant: ' + (req.subdomains[0] || 'none'));
});❓ FAQ
It is an array of subdomain segments parsed from the request host.
It uses the subdomain offset setting to ignore top-level domain segments.
By default Express uses offset 2 (common for domains like example.com).
Yes. Use app.set('subdomain offset', value) for your domain structure.
Yes, it is often used to resolve tenant context from subdomain-based URLs.
Did you know?
req.subdomains returns subdomain parts from the host name, controlled by Express subdomain offset.
4 people found this page helpful
