Express req.hostname Property

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 4 Code Examples

What you’ll learn

  • How req.hostname is derived from request headers.
  • How it differs from req.host and req.get('host').
  • How proxy trust settings influence hostname parsing.
  • How to use hostname checks safely in multi-domain apps.

Usage syntax

javascript
req.hostname
1

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.

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.

4 people found this page helpful