ExpressJS: What is the difference between app.local and res.local?
Asked Answered
M

2

11

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!

Mighty answered 25/9, 2012 at 20:19 Comment(0)
I
10

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')

Izaguirre answered 26/9, 2012 at 17:9 Comment(4)
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.Mighty
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.Izaguirre
One might use app.locals.site_url = 'example.com'; It would be easier than assigning to res.locals.site_url inside every route.Izaguirre
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?Mcminn
F
14

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

Foppery answered 5/6, 2014 at 3:21 Comment(0)
I
10

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')

Izaguirre answered 26/9, 2012 at 17:9 Comment(4)
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.Mighty
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.Izaguirre
One might use app.locals.site_url = 'example.com'; It would be easier than assigning to res.locals.site_url inside every route.Izaguirre
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?Mcminn

© 2022 - 2024 — McMap. All rights reserved.