req.hostname
The hostname derived from the Host (or X-Forwarded-Host) header.
Syntax
req.hostname Returns
string — The request hostname without the port.
Examples
app.get('/host', (req, res) => {
res.send(req.hostname);
});
Output
$ curl -H 'Host: api.example.com' localhost:3000/host
api.example.com
app.use((req, res, next) => {
if (req.hostname === 'admin.example.com') req.isAdminHost = true;
next();
});
Notes
Strips the port. When `trust proxy` is enabled Express uses
`X-Forwarded-Host`. Validate or whitelist expected hostnames if you
branch logic on it, since the Host header is client-controlled.