app.js
This file
Path
examples/form/app.js
79 lines · 2.1 KiB · JavaScript
Last changed by
Updated form example for 3.x (not working as it relies on req.flash())Robert Sköldbefore adoption7cb1c3c
History of this fileTake it
The walk stopped after 200 commits: lines older than f98c112 point at it.
Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 1–4why, one step back1/**2 * Module dependencies.3 */4Updated form example for 3.x (not working as it relies on req.flash())Robert Sköldbefore adoption7cb1c3c
line 5why, one step back5var express = require('../../');Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
line 6why, one step back6Updated form example for 3.x (not working as it relies on req.flash())Robert Sköldbefore adoption7cb1c3c
line 7why, one step back7var app = module.exports = express();Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 8–75why, one step back89// Here we use the bodyDecoder middleware10// to parse urlencoded request bodies11// which populates req.body12app.use(express.bodyParser());13 14// The methodOverride middleware allows us15// to set a hidden input of _method to an arbitrary16// HTTP method to support app.put(), app.del() etc17app.use(express.methodOverride());1819// Required by session20app.use(express.cookieParser());2122// Required by req.flash() for persistent23// notifications24app.use(express.session({ secret: 'keyboard cat' }));2526app.get('/', function(req, res){27 // get ?name=foo28 var name = req.param('name') || '';29 30 // Switch the button label based if we have a name31 var label = name ? 'Update' : 'Save';3233 // Buffer all flash messages.34 // Typically this would all be done in a template35 // however for illustration purposes we iterate36 // here.37 38 // The messages in req.flash() persist until called,39 // at which time they are flushed from the session40 var msgs = '<ul>',41 flash = req.flash();42 Object.keys(flash).forEach(function(type){43 flash[type].forEach(function(msg){44 msgs += '<li class="' + type + '">' + msg + '</li>';45 });46 });47 msgs += '</ul>';4849 // If we have a name, we are updating,50 // so add the hidden _method input51 res.send(msgs52 + '<form method="post">'53 + (name ? '<input type="hidden" value="put" name="_method" />' : '')54 + 'Name: <input type="text" name="name" value="' + name + '" />'55 + '<input type="submit" value="' + label + '" />'56 + '</form>');57});5859app.post('/', function(req, res){60 if (req.body.name) {61 // Typically here we would create a resource62 req.flash('info', 'Saved ' + req.body.name);63 res.redirect('/?name=' + req.body.name);64 } else {65 req.flash('error', 'Error: name required');66 res.redirect('/');67 }68});6970app.put('/', function(req, res){71 // Typically here we would update a resource72 req.flash('info', 'Updated ' + req.body.name);73 res.redirect('/?name=' + req.body.name);74});75Updated form example for 3.x (not working as it relies on req.flash())Robert Sköldbefore adoption7cb1c3c
lines 76–79why, one step back76if (!module.parent) {77 app.listen(3000);78 console.log('Express started on port 3000');79}