app.js
This file
Path
examples/flash/app.js
78 lines · 2.3 KiB · JavaScript
Last changed by
Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
History of this fileTake it
Merge branch 'flash-example'Tj Holowaychukbefore adoption7dff9dd
line 1why, one step back1Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
lines 2–4why, one step back2// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');4Merge branch 'flash-example'Tj Holowaychukbefore adoption7dff9dd
lines 5–31why, one step back5/**6 * Module dependencies.7 */89var express = require('../../lib/express');1011// App with session support1213var app = express.createServer(14 express.cookieDecoder(),15 express.session()16);1718// View settings1920app.set('views', __dirname + '/views');21app.set('view engine', 'ejs');2223// Dynamic helpers are functions which are executed24// on each view render, unless dynamicHelpers is false.2526// So for example we do not need to call messages() in our27// template, "messages" will be populated with the return28// value of this function.2930app.dynamicHelpers({31 messages: function(req, res){Fixed flash example. Closes #392Tj Holowaychukbefore adoptionae1c20f
lines 32–55why, one step back32 // In the case of flash messages33 // we return a function, allowing34 // flash messages to only be flushed35 // when called, otherwise every request36 // will flush flash messages regardless.37 return function(){38 // Grab the flash messages39 var messages = req.flash();40 // We will render the "messages.ejs" partial41 return res.partial('messages', {42 // Our target object is our messages43 object: messages,44 // We want it to be named "types" in the partial45 // since they are keyed like this:46 // { info: ['foo'], error: ['bar']}47 as: 'types',48 // Pass a local named "hasMessages" so we can easily49 // check if we have any messages at all50 locals: { hasMessages: Object.keys(messages).length },51 // We dont want dynamicHelpers in this partial, as52 // it would cause infinite recursion53 dynamicHelpers: false54 });55 }Merge branch 'flash-example'Tj Holowaychukbefore adoption7dff9dd
lines 56–58why, one step back56 }57});58Added another dynamicHelper example to flash exampleTj Holowaychukbefore adoptionfd9a245
lines 59–68why, one step back59app.dynamicHelpers({60 // Another dynamic helper example. Since dynamic61 // helpers resolve at view rendering time, we can62 // "inject" the "page" local variable per request63 // providing us with the request url.64 page: function(req, res){65 return req.url;66 } 67});68Merge branch 'flash-example'Tj Holowaychukbefore adoption7dff9dd
lines 69–77why, one step back69app.get('/', function(req, res){70 // Not very realistic notifications but illustrates usage71 req.flash('info', 'email queued');72 req.flash('info', 'email sent');73 req.flash('error', 'delivery failed');74 res.render('index');75});7677app.listen(3000);Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
line 78why, one step back78console.log('Express app started on port 3000');