Skip to content
Myr

app.js

This file

Path

examples/flash/app.js

78 lines · 2.2 KiB · JavaScript

Last changed by

fixed flash exampleTj Holowaychukbefore adoption322300c

History of this file

Take it

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

Regenerated guideTj Holowaychukbefore adoptionf179979

lines 1–13why, 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(

fixed flash exampleTj Holowaychukbefore adoption322300c

lines 14–15why, one step back
14 express.cookieDecoder()
15 , express.session()

Regenerated guideTj Holowaychukbefore adoptionf179979

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({

fixed flash exampleTj Holowaychukbefore adoption322300c

lines 31–54why, one step back
31 messages: function(req, res){
32 // 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 });

Regenerated guideTj Holowaychukbefore adoptionf179979

line 55why, one step back
55 }

fixed flash exampleTj Holowaychukbefore adoption322300c

line 56why, one step back
56 }

Regenerated guideTj Holowaychukbefore adoptionf179979

lines 57–59why, one step back
57});
5859app.dynamicHelpers({

fixed flash exampleTj Holowaychukbefore adoption322300c

lines 60–66why, one step back
60 // Another dynamic helper example. Since dynamic
61 // 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 }

Regenerated guideTj Holowaychukbefore adoptionf179979

lines 67–69why, one step back
67});
6869app.get('/', function(req, res){

fixed flash exampleTj Holowaychukbefore adoption322300c

lines 70–74why, one step back
70 // Not very realistic notifications but illustrates usage
71 req.flash('info', 'email queued');72 req.flash('info', 'email sent');73 req.flash('error', 'delivery failed');74 res.render('index');

Regenerated guideTj Holowaychukbefore adoptionf179979

lines 75–78why, one step back
75});
7677app.listen(3000);78console.log('Express app started on port 3000');