app.js
This file
Path
examples/form/app.js
79 lines · 2.2 KiB · JavaScript
Last changed by
Cleaning up form exampleTj Holowaychukbefore adoption656e67e
History of this fileTake it
Added form exampleTj Holowaychukbefore adoption018d865
lines 1–9why, one step back12/**3 * Module dependencies.4 */56var express = require('./../../lib/express'),7 connect = require('connect'),8 sys = require('sys');9Cleaning up form exampleTj Holowaychukbefore adoption656e67e
lines 10–15why, one step back10var app = express.createServer();1112// Here we use the bodyDecoder middleware13// to parse urlencoded request bodies14// which populates req.body15app.use(connect.bodyDecoder());Added form exampleTj Holowaychukbefore adoption018d865
line 16why, one step back16 Cleaning up form exampleTj Holowaychukbefore adoption656e67e
lines 17–20why, one step back17// The methodOverride middleware allows us18// to set a hidden input of _method to an arbitrary19// HTTP method to support app.put(), app.del() etc20app.use(connect.methodOverride());Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
line 21why, one step back21Cleaning up form exampleTj Holowaychukbefore adoption656e67e
lines 22–23why, one step back22// Required by session23app.use(connect.cookieDecoder());Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
line 24why, one step back24Cleaning up form exampleTj Holowaychukbefore adoption656e67e
lines 25–27why, one step back25// Required by req.flash() for persistent26// notifications27app.use(connect.session());Added form exampleTj Holowaychukbefore adoption018d865
lines 28–35why, one step back2829app.get('/', function(req, res){30 // get ?name=foo31 var name = req.param('name') || '';32 33 // Switch the button label based if we have a name34 var label = name ? 'Update' : 'Save';35Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
lines 36–51why, one step back36 // Buffer all flash messages.37 // Typically this would all be done in a template38 // however for illustration purposes we iterate39 // here.40 41 // The messages in req.flash() persist until called,42 // at which time they are flushed from the session43 var msgs = '<ul>',44 flash = req.flash();45 Object.keys(flash).forEach(function(type){46 flash[type].forEach(function(msg){47 msgs += '<li class="' + type + '">' + msg + '</li>';48 });49 });50 msgs += '</ul>';51Added form exampleTj Holowaychukbefore adoption018d865
lines 52–53why, one step back52 // If we have a name, we are updating,53 // so add the hidden _method inputAdded req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
lines 54–55why, one step back54 res.send(msgs55 + '<form method="post">'Added form exampleTj Holowaychukbefore adoption018d865
lines 56–62why, one step back56 + (name ? '<input type="hidden" value="put" name="_method" />' : '')57 + 'Name: <input type="text" name="name" value="' + name + '" />'58 + '<input type="submit" value="' + label + '" />'59 + '</form>');60});6162app.post('/', function(req, res){Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
lines 63–70why, one step back63 if (req.body.name) {64 // Typically here we would create a resource65 req.flash('info', 'Saved ' + req.body.name);66 res.redirect('/?name=' + req.body.name);67 } else {68 req.flash('error', 'Error: name required');69 res.redirect('/');70 }Added form exampleTj Holowaychukbefore adoption018d865
lines 71–74why, one step back71});7273app.put('/', function(req, res){74 // Typically here we would update a resourceAdded req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8
line 75why, one step back75 req.flash('info', 'Updated ' + req.body.name);Added form exampleTj Holowaychukbefore adoption018d865
lines 76–79why, one step back76 res.redirect('/?name=' + req.body.name);77});7879app.listen(3000);