I'm using Nodejs and Expressjs and Kraken, I need to display message when added a product on index but I tried many time for to config but messages still not appear as I expect. Here is my config.js:
var flash = require('connect-flash');
app = module.exports = express();
app.use(kraken(options));
//flash
app.use(flash());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
My controller :
router.post('/somePath', function (req, res) {
//something to do to add
res.flash('messages','Add success!!')
res.render('path/index');
});
My index.dust file:
`{>"layouts/master" /}
{<body}
{messages|s}
// body goes here
{/body}
`
You're pretty close to the answer.
This line
Stores a function in
messagesthat outputs the flash messages as an html fragment.res.localsis merged by express with the models that are used to render your template.Now you just need a way to invoke this function from within the dust template.
Doing this:
Doesn't actually invoke the function. You need to call it as if it were a context helper:
You'll have one last hurdle to clear. The function signature that
express-messagesexpects, is different from what dust provides, so you'll have to wrap it within a helper function (in your server.js file):