Updated Express.js from version 2 to 3, and the following call to app.dynamicHelpers({..})
broke as it is no longer present in V3:
app.dynamicHelpers({
request: function(req){
return req
},
...etc.
});
There's a migration guide which says this:
app.dynamicHelpers()
(use middleware + res.locals)But I'm stumped how to do that. Is there a more concrete example of how to migrate that?
Related SO post: nodejs express 3.0
I had the same problem with session.user and just fixed it by understanding that the app.use function needs to be IN the configure part, not where it was before.
Before:
app.configure();
app.dynamicHelpers({
user: function(req, res) {
return req.session.user;
}
});
After:
app.configure(function(){
//...
app.use(function(req, res, next){
res.locals.user = req.session.user;
next();
});
//...
});
for Flash have a look at connect-flash
The solution with 16 votes is correct but be sure to use the res.locals
assignment before
app.use(app.router);
refer to this post https://stackoverflow.com/a/12597730/1132109
Have a look at the examples folder at github. For example auth:
app.use(function(req, res, next){
var err = req.session.error,
msg = req.session.success;
delete req.session.error;
delete req.session.success;
res.locals.message = '';
if (err) res.locals.message = '<p class="msg error">' + err + '</p>';
if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';
next();
});
You can then use the variable "message" in your template.
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