req.headers
An object of the incoming request HTTP headers.
Syntax
req.headers Returns
object — Header names (lowercased) mapped to values.
Examples
app.get('/ua', (req, res) => {
res.send(req.headers['user-agent']);
});
Output
$ curl localhost:3000/ua
curl/8.4.0
app.get('/auth', (req, res) => {
const token = req.headers.authorization?.split(' ')[1];
res.json({ hasToken: Boolean(token) });
});
Output
{"hasToken":true}
Notes
Header keys are always lowercased. Prefer `req.get('Header-Name')`
for case-insensitive single-header access. Do not trust
client-supplied headers like `X-Forwarded-For` unless `trust proxy`
is configured for your known proxy.