Access app settings
Read application configuration inside a response handler without importing the app instance.
Express.js · Pipe
res.app references the Express application instance handling the current response. It provides access to application configuration and is the same object exposed as req.app.
res.app exposes the Express application instance associated with the current response. Use it to access application-level configuration and methods from response middleware or route handlers; it refers to the same application available as req.app.
res.appApplication instance
req.app reference.res.app, including the configured view engine.res.app and req.app as separate application objects; they reference the same instance.import express from 'express';
const app = express();
app.get('/settings', (req, res) => {
const sameApp = res.app === req.app;
res.json({
viewEngine: res.app.get('view engine'),
sameApp
});
});
app.listen(3000);This route reads application settings through the application instance referenced by the response.
Read application configuration inside a response handler without importing the app instance.
Check whether request and response objects point to the same Express application.
Use application methods from middleware that receives only the response object.
res.app represent?res.app is a response property containing the Express application instance that is handling the request. It lets response-side code access application configuration and methods, such as get().
Follow-up: How can you verify it matches the request-side application? Compare res.app === req.app; the documentation’s example shows this evaluates to true.
res.app, and how might it be used?Its documented type is Application. In a handler, calling res.app.get('view engine') reads an application setting through the instance associated with the response.
Follow-up: Why might this be useful in middleware? Middleware can access application-level configuration without importing the app object directly.
res.app relate to req.app?res.app and req.app refer to the same Express application instance for the current request. Therefore, either property can be used to reach that application from the corresponding object.
Follow-up: What should code avoid assuming? It should not assume the property identifies a new or separate app; it is the current request’s application reference.
req.appApplicationapp.get