Res.redirect and locals

1,058 views
Skip to first unread message

Andrea Di Mario

unread,
Nov 19, 2012, 3:33:07 AM11/19/12
to expre...@googlegroups.com
Hi, i'm trying to set locals when use res.redirect, to display message on view, i'm trying to add locals and use res.locals, without results. Could you tell me if express allow that?

Thanks for your time.
Best regards.

David Dripps

unread,
Nov 19, 2012, 12:19:35 PM11/19/12
to expre...@googlegroups.com
Are you talking about Flash messages? I'd suggest using connect-flash

If you want to do it manually, then store your flash messages on req.session.flashMessages or something and then check for them in your middleware. If they exist, assign them to res.locals in your middleware so they're added on the redirect request.

David

Andrea Di Mario

unread,
Nov 19, 2012, 12:46:09 PM11/19/12
to expre...@googlegroups.com
Hi David, thanks for your replay, i've looked connect-flash solution but i should rewrite something on my app's message system. Maybe i'll try your suggestion about a session variable, or something with connect-flash.
It would be useful a locals setting in res.redirect too, as res.render that could made easy this operation.

Best regards

Ryan Schmidt

unread,
Nov 19, 2012, 4:41:06 PM11/19/12
to expre...@googlegroups.com
It seems to me that it's already as easy as it can be. res.locals is for local variables related to the response to the current request. If your response is a redirect, then that is the end of the response and the locals go away. The browser will then make a second request (for the redirected address), and your app will then deliver a second response, which could have its own locals. HTTP is not stateful -- there is no relationship between one request and the next. Any such relationship we create in our applications is a fiction held together by a unique ID sent as a cookie, a mechanism which is commonly called a session. If you want variables to persist from one request to another, put them in the session. If you don't want to manage this yourself, use a module that does, like presumably connect-flash.

Andrea Di Mario

unread,
Nov 20, 2012, 3:40:45 AM11/20/12
to expre...@googlegroups.com
Hi, understood, i'm working on res.locals, i've set it but i see that this value is static, because error message don't disappear if i unset session variable. I'll study this situation..

Best regards.
Reply all
Reply to author
Forward
0 new messages