Express req.host Property
What you’ll learn
- What value
req.hostreturns in Express. - How Host and forwarded host headers affect output.
- How
req.hostdiffers fromreq.hostname. - How to safely use host information in redirects and links.
Usage syntax
javascript
req.host
req.hostname1
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.
4 people found this page helpful
