req.get()
Returns the value of a request header by name, case-insensitively.
Syntax
req.get(field) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
field | string | Yes | The header name to read (case-insensitive). |
Returns
string | undefined — The header value, or undefined if absent.
Examples
app.get('/ct', (req, res) => {
res.send(req.get('Content-Type') ?? 'none');
});
Output
$ curl -H 'Content-Type: application/json' localhost:3000/ct
application/json
app.use((req, res, next) => {
const token = req.get('Authorization')?.replace('Bearer ', '');
req.token = token;
next();
});
Notes
Case-insensitive, so `req.get('content-type')` and
`req.get('Content-Type')` are equivalent. `req.header()` is an
alias. `Referer` and `Referrer` are treated as the same field.