req.app

req.app

This property holds a reference to the instance of the Express application that is using the middleware.

If you follow the pattern in which you create a module that just exports a middleware function and require() it in your main file, then the middleware can access the Express instance via req.app

For example:

1
2
//index.js
app.get('/viewdirectory', require('./mymiddleware.js'))
1
2
3
4
//mymiddleware.js
module.exports = function (req, res) {
  res.send('The views directory is ' + req.app.get('views'));
});
doc_Express
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.