Skip to content
Myr

app.js

This file

Path

examples/flash/app.js

83 lines · 2.3 KiB · JavaScript

Last changed by

docsTj Holowaychukbefore adoption6cae1f0

History of this file

Take it

The walk stopped after 200 commits: lines older than 8c163fd point at it.

Updated connect-form submoduleTj Holowaychukbefore adoption8c163fd

lines 1–14why, one step back
1
2// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */89var express = require('../../lib/express');1011// App with session support1213var app = express.createServer(14 express.cookieDecoder()

Added secret to session middleware used in examples and generated appTj Holowaychukbefore adoption7687299

line 15why, one step back
15 , express.session({ secret: 'keyboard cat' })

Updated connect-form submoduleTj Holowaychukbefore adoption8c163fd

lines 16–30why, one step back
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({

docsTj Holowaychukbefore adoption6cae1f0

lines 31–35why, one step back
31 // as an alternative to this install the
32 // express-messages helper via npm33 // 34 // $ npm install express-messages35 //

Updated connect-form submoduleTj Holowaychukbefore adoption8c163fd

lines 36–83why, one step back
36 messages: function(req, res){
37 // In the case of flash messages38 // we return a function, allowing39 // flash messages to only be flushed40 // when called, otherwise every request41 // will flush flash messages regardless.42 return function(){43 // Grab the flash messages44 var messages = req.flash();45 // We will render the "messages.ejs" partial46 return res.partial('messages', {47 // Our target object is our messages48 object: messages,49 // We want it to be named "types" in the partial50 // since they are keyed like this:51 // { info: ['foo'], error: ['bar']}52 as: 'types',53 // Pass a local named "hasMessages" so we can easily54 // check if we have any messages at all55 locals: { hasMessages: Object.keys(messages).length },56 // We dont want dynamicHelpers in this partial, as57 // it would cause infinite recursion58 dynamicHelpers: false59 });60 }61 }62});6364app.dynamicHelpers({65 // Another dynamic helper example. Since dynamic66 // helpers resolve at view rendering time, we can67 // "inject" the "page" local variable per request68 // providing us with the request url.69 page: function(req, res){70 return req.url;71 } 72});7374app.get('/', function(req, res){75 // Not very realistic notifications but illustrates usage76 req.flash('info', 'email queued');77 req.flash('info', 'email sent');78 req.flash('error', 'delivery failed');79 res.render('index');80});8182app.listen(3000);83console.log('Express app started on port 3000');