Express res.get() Method
What you’ll learn
- How to read outgoing response headers with
res.get(). - How
res.get()differs fromreq.get(). - How to use header checks in middleware flow.
- When header lookups can return
undefined.
Syntax
javascript
res.get(field)1
Read a header after setting it
javascript
app.get('/cache', function (req, res) {
res.set('Cache-Control', 'public, max-age=60');
var cacheHeader = res.get('Cache-Control');
res.json({ cache: cacheHeader });
});2
Inspect headers in middleware chain
javascript
app.use(function (req, res, next) {
res.set('X-App-Version', '1.0.0');
next();
});
app.get('/version', function (req, res) {
res.send('Version header: ' + res.get('X-App-Version'));
});3
Handle missing header value safely
javascript
app.get('/content-type-check', function (req, res) {
var contentType = res.get('Content-Type');
if (!contentType) return res.type('text').send('Content-Type not set yet');
res.send('Content-Type: ' + contentType);
});⚠️ Common pitfalls
- Do not confuse
req.get()(incoming headers) withres.get()(outgoing headers). - Expect
undefinedwhen reading headers that were never set. - Avoid mutating response state after the response has already been sent.
❓ FAQ
It returns the current value of a response header that has been set on res.
No. req.get() reads incoming request headers; res.get() reads outgoing response headers.
You can, but it usually returns undefined if that header has not been set yet.
Avoid relying on response mutations after send/end; inspect or set headers before finishing the response.
Yes, it is useful for checking headers set by earlier middleware before deciding next steps.
Did you know?
res.get(field) reads a response header currently set on the outgoing response object.
4 people found this page helpful
