Express req.hostname Property
What you’ll learn
- How
req.hostnameis derived from request headers. - How it differs from
req.hostandreq.get('host'). - How proxy trust settings influence hostname parsing.
- How to use hostname checks safely in multi-domain apps.
Usage syntax
javascript
req.hostname1
Get normalized hostname value
javascript
app.get('/tenant', function (req, res) {
var host = req.hostname;
res.send('Hostname is: ' + host);
});2
Route logic by hostname
javascript
app.get('/dashboard', function (req, res) {
if (req.hostname === 'admin.example.com') {
return res.send('Admin dashboard');
}
res.send('User dashboard');
});❓ FAQ
It gives the hostname part of the request host, excluding port and protocol.
req.hostname is normalized hostname-only, while req.host may include additional host header details like port.
Yes. With trust proxy enabled, forwarded host information can affect the computed hostname.
Yes, often it is better for subdomain-based routing, but still validate against allowed domains.
Yes. For example, api.example.com remains api.example.com.
Did you know?
req.hostname returns only the hostname part (without port), making it safer than raw host header strings for many checks.
4 people found this page helpful
