/** * Module dependencies. */ var express = require('./../../lib/express'), sys = require('sys'); var app = express.createServer(); // Here we use the bodyDecoder middleware // to parse urlencoded request bodies // which populates req.body app.use(express.bodyDecoder()); // The methodOverride middleware allows us // to set a hidden input of _method to an arbitrary // HTTP method to support app.put(), app.del() etc app.use(express.methodOverride()); // Required by session app.use(express.cookieDecoder()); // Required by req.flash() for persistent // notifications app.use(express.session()); app.get('/', function(req, res){ // get ?name=foo var name = req.param('name') || ''; // Switch the button label based if we have a name var label = name ? 'Update' : 'Save'; // Buffer all flash messages. // Typically this would all be done in a template // however for illustration purposes we iterate // here. // The messages in req.flash() persist until called, // at which time they are flushed from the session var msgs = ''; // If we have a name, we are updating, // so add the hidden _method input res.send(msgs + '
' + (name ? '' : '') + 'Name: ' + '' + '
'); }); app.post('/', function(req, res){ if (req.body.name) { // Typically here we would create a resource req.flash('info', 'Saved ' + req.body.name); res.redirect('/?name=' + req.body.name); } else { req.flash('error', 'Error: name required'); res.redirect('/'); } }); app.put('/', function(req, res){ // Typically here we would update a resource req.flash('info', 'Updated ' + req.body.name); res.redirect('/?name=' + req.body.name); }); app.listen(3000);