I have a middleware (in sails.js, an express based framework) that looks something up in a database and then stores what it found in an object, so I can use it in layouts/controllers.
Currently, I'm storing it in the req object, like so:
req.someStuff = 'Some stuff';
However, I just read that you can also do:
res.locals.someStuff = 'Some stuff';
What's the difference? Both manage to accomplish the same task. Thanks.
See the docs here .
Basically, you pass res.locals to the rendering engine (say you're using jade, ejs, etc..). When it renders, it becomes available to the global scope, so you can just call them as named (ie. someStuff as opposed to locals.someStuff).
res.someStuff, would therefore not be available to the global scope when rendered, so cannot be accessed by whatever rendering template your using (ie. cannot be called as someStuff).
In a nutshell it just gives an easy way to pass something to a rendering template and have it accessible by the global scope.
An example (using ejs) would be something like
<% if(someStuff) { ... } %>
<!-- or even this -->
<% if(locals.someStuff) { ... } %>
Edit - I also just realized you're posting the req.someStuff vs res.someStuff. You can read through those same docs to see the difference between those, but basically one is the incoming request object, the other is the outgoing response object. (It's http so request-response protocol)