Adding custom properties to Express app and req. What's the best way?
express, javascript, node.js
Solution
I would recommend attaching a single property to app that's probably never going to conflict, and accessing everything from there, like `app.myLibrary`.
app.myLibrary = {config: config, core: core, mean: meanModules};
And access app.myLibrary from within routes/middleware:
req.app.myLibrary
Unless something dynamic is happening in the middleware that varies per request, it's likely better to just access it with `req.app.myLibrary`.
Problem
Am I shooting myself in the foot: I want to make config, core, and mean available on the app and req objects in my Express app. I'm using properties not in the 4.x API. Anything I should know? Is there a problem with just adding them as properties? ``` // express.js module.exports = function(db, config, meanModules) { var app = express(); // ... // Get mean-core var core = require('meanjs-core')(db, config); // Attach config, core, and modules to app <==== POSSIBLE FOOT SHOOTING app.config = config; app.core = core; app.mean = meanModules; // Middleware to adjust req app.use(function(req, res, next) { // Add config, core, and modules to all requests <==== POSSIBLE FOOT SHOOTING req.config = config; req.core = core; req.mean = meanModules; next(); }); // ... } ```