app.js
This file
Path
examples/form/app.js
78 lines · 2.2 KiB · JavaScript
Last changed by
Updated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f
History of this fileTake it
The walk stopped after 200 commits: lines older than 51611ec point at it.
Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 1–8why, one step back12/**3 * Module dependencies.4 */56var express = require('./../../lib/express'),7 sys = require('sys');8Cleaning up form exampleTj Holowaychukbefore adoption656e67e
lines 9–13why, one step back9var app = express.createServer();1011// Here we use the bodyDecoder middleware12// to parse urlencoded request bodies13// which populates req.bodyUpdated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f
line 14why, one step back14app.use(express.bodyDecoder());Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
line 15why, one step back15 Cleaning up form exampleTj Holowaychukbefore adoption656e67e
lines 16–18why, one step back16// The methodOverride middleware allows us17// to set a hidden input of _method to an arbitrary18// HTTP method to support app.put(), app.del() etcUpdated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f
line 19why, one step back19app.use(express.methodOverride());Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
line 20why, one step back20Cleaning up form exampleTj Holowaychukbefore adoption656e67e
line 21why, one step back21// Required by sessionUpdated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f
line 22why, one step back22app.use(express.cookieDecoder());Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
line 23why, one step back23Cleaning up form exampleTj Holowaychukbefore adoption656e67e
lines 24–25why, one step back24// Required by req.flash() for persistent25// notificationsUpdated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f
line 26why, one step back26app.use(express.session());Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 27–34why, one step back2728app.get('/', function(req, res){29 // get ?name=foo30 var name = req.param('name') || '';31 32 // Switch the button label based if we have a name33 var label = name ? 'Update' : 'Save';34Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
lines 35–50why, one step back35 // Buffer all flash messages.36 // Typically this would all be done in a template37 // however for illustration purposes we iterate38 // here.39 40 // The messages in req.flash() persist until called,41 // at which time they are flushed from the session42 var msgs = '<ul>',43 flash = req.flash();44 Object.keys(flash).forEach(function(type){45 flash[type].forEach(function(msg){46 msgs += '<li class="' + type + '">' + msg + '</li>';47 });48 });49 msgs += '</ul>';50Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 51–52why, one step back51 // If we have a name, we are updating,52 // so add the hidden _method inputAdded req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
lines 53–54why, one step back53 res.send(msgs54 + '<form method="post">'Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 55–61why, one step back55 + (name ? '<input type="hidden" value="put" name="_method" />' : '')56 + 'Name: <input type="text" name="name" value="' + name + '" />'57 + '<input type="submit" value="' + label + '" />'58 + '</form>');59});6061app.post('/', function(req, res){Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
lines 62–69why, one step back62 if (req.body.name) {63 // Typically here we would create a resource64 req.flash('info', 'Saved ' + req.body.name);65 res.redirect('/?name=' + req.body.name);66 } else {67 req.flash('error', 'Error: name required');68 res.redirect('/');69 }Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 70–73why, one step back70});7172app.put('/', function(req, res){73 // Typically here we would update a resourceAdded req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
line 74why, one step back74 req.flash('info', 'Updated ' + req.body.name);Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 75–78why, one step back75 res.redirect('/?name=' + req.body.name);76});7778app.listen(3000);