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();
});
// ...
}
The req. query property is an object containing the property for each query string parameter in the route.
The req. body object allows you to access data in a string or JSON object from the client side. You generally use the req. body object to receive data through POST and PUT requests in the Express server.
The req object represents the HTTP request and has properties for the request query string, parameters, body, and HTTP headers. The res object represents the HTTP response that an Express app sends when it gets an HTTP request. In our case, we are sending a text Hello World whenever a request is made to the route / .
app. get is called when the HTTP method is set to GET , whereas app. use is called regardless of the HTTP method, and therefore defines a layer which is on top of all the other RESTful types which the express packages gives you access to.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With