10

I'm trying to learn Express and in my app I have middleware that passes the session object from the Request object to my Response object so that I can access it in my views:

app.use((req, res, next) ->
  res.locals.session = req.session
  next()
)

But app.locals is available to the view as well right? So is it the same if I do app.locals.session = req.session? Is there a convention for the types of things app.locals and res.locals are used for?

I was also confused on what the difference is between res.render() and res.redirect()? When should each be used?

Thanks for reading. Any help related to Express is appreciated!

chovy
  • 72,281
  • 52
  • 227
  • 295
aeyang
  • 807
  • 4
  • 9
  • 18

2 Answers2

13

To illustrate this further, I remember viewing a flowchart which shows how express renders variables found inside a template. This is from "Node.js In Action." I recommend reading the chapter discussing Express.js.

enter image description here

Michael
  • 6,561
  • 5
  • 38
  • 55
9

app.locals and res.locals can be used in different contexts.

res.locals is for when you handle the route where you have a res object, you won't have an app object there and vice-versa for app.locals.

also res.render will render the page, to handle the request. res.redirect will redirect them to a different page.

For example if they try to access /account without logging in, you could flash a message and use res.redirect('/login')

chovy
  • 72,281
  • 52
  • 227
  • 295
  • Thanks for your post. I have a few questions about what you wrote: First, can we generally say that the information that resides in res.locals has to do with routes, and user response data (i.e. what users POST and results from database calls)? What kinds of information should reside in app.locals? Just looking for simple examples so I can get an idea of how this works. And thanks for the explanation on `res.redirect()`, that was very helpful. – aeyang Sep 29 '12 at 21:33
  • They are the same thing. If you set res.locals.foo = 'foo' and app.locals.foo = 'foo' -- they will both be available in your template as "<%= foo %>". The only difference is context. In your app.js file where you setup the app() stuff, you can define locals there, this would persist on every page giving all your templates access to the variable. On the other hand, with res.locals inside your routes callbak, you could do res.locals.foo = 'foo' -- this can be done just for one route, not all of them. That is the big difference. All requests with app.locals, or just one request with res.locals. – chovy Sep 29 '12 at 21:45
  • One might use app.locals.site_url = 'http://example.com'; It would be easier than assigning to res.locals.site_url inside every route. – chovy Sep 29 '12 at 21:48
  • If I understood well, we should use APP.LOCALS within our app.use(..) middleware. RES.LOCALS are reserved for the routes (app.get(..)). Correct? So if I define app.locals inside my middleware it should be available in all my routes? Is that safe? – miksiii Feb 10 '15 at 22:42