I have a notifications system for my website (set up in Node.js) and I’m using an MVC pattern. In my controllers, I do a check if there’s a flash message:
if flashMessages = req.flash()
if flashMessages.gritterMsgs
for flashMessage in flashMessages.gritterMsgs
The only issue is I have to do this for every controller and every function within that controller. Is there someway to include this once (perhaps as middleware)?
Express already has view helpers, you can setup the session and flash objects for views like so:
Then in your views you can directly use the
flashobject (which is set in the controller like so:req.flash = "my flash stuff".Here’s an example: https://github.com/alessioalex/Nodetuts/blob/master/express_samples/app.js