Express res.app Property
What you’ll learn
- What
res.appis and where it is available. - How to read app-level settings from response context.
- How
res.appcompares withreq.app. - How to avoid misusing shared app state.
Syntax
javascript
res.app
res.app.get('trust proxy')1
Read app setting from response context
javascript
app.set('siteName', 'CodeToFun');
app.get('/info', function (req, res) {
res.json({ site: res.app.get('siteName') });
});2
Use app-level shared service
javascript
app.set('logger', console);
app.use(function (req, res, next) {
var logger = res.app.get('logger');
logger.log('Request path:', req.path);
next();
});3
res.app vs req.app
javascript
app.get('/same-app', function (req, res) {
res.json({ same: req.app === res.app });
});⚠️ Common pitfalls
- Do not store request-specific data in
app; usereqorres.locals. - Avoid mutating app-wide settings inside request handlers.
- Prefer dependency injection patterns for larger apps instead of scattered
app.get()calls.
❓ FAQ
res.app is a reference from the response object to the current Express app instance.
There is no app-instance difference; both properties reference the same app.
Yes, use res.app.get('settingName') for settings created with app.set().
Use it when your code only has response context but needs app-level config/services.
No, keep per-request data on req or res.locals, not the shared app object.
Did you know?
res.app and req.app both point to the same Express application instance.
4 people found this page helpful
