Express req.res Property
What you’ll learn
- What
req.respoints to in Express internals. - How it relates to the
resargument in handlers. - When using
req.resis acceptable vs avoidable. - How to keep middleware code readable and testable.
Usage syntax
javascript
req.res
res1
Access response via req.res (equivalent behavior)
javascript
app.use(function (req, res, next) {
req.res.set('X-Request-Mode', 'middleware');
next();
});2
Prefer direct res argument for clarity
javascript
app.get('/status', function (req, res) {
res.json({ ok: true, method: req.method });
});❓ FAQ
It is a reference from the request object to the current response object.
Rarely. In most middleware/route handlers, use the res argument directly for readability.
They point to the same response object instance during the request lifecycle.
It can be, but passing explicit dependencies is often cleaner and easier to test.
No. It behaves like using res directly, since both refer to the same object.
Did you know?
req.res references the current response object, but using res directly in handlers is usually clearer.
4 people found this page helpful
