Express req.host Property

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

What you’ll learn

  • What value req.host returns in Express.
  • How Host and forwarded host headers affect output.
  • How req.host differs from req.hostname.
  • How to safely use host information in redirects and links.

Usage syntax

javascript
req.host
req.hostname
1

Read host from incoming request

javascript
app.get('/where-am-i', function (req, res) {
  res.json({
    host: req.host,
    hostname: req.hostname
  });
});
2

Build absolute URL from request context

javascript
app.get('/self', function (req, res) {
  var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
  res.send(fullUrl);
});

❓ FAQ

It returns the request host name from Host-related headers, without protocol.
req.host may include host and port information, while req.hostname returns only the hostname portion.
Yes. With trust proxy enabled, forwarded host headers can influence the value.
Only with careful validation and proper proxy configuration, because headers can be spoofed.
Combine protocol and host, for example req.protocol + '://' + req.get('host').
Did you know?

req.host is derived from the Host header (or X-Forwarded-Host with trusted proxy), and excludes the protocol.

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