refactored form example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 322300c · Browse files · 1 file·+38 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+38 added−38 removed
- other:+38 added−38 removed
Provenance
- Commit
c1cfd4a- Tree
6cc58e8- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
322300c
1 file changed+38 added−38 removed
modified:examples/form/app.js+38 added-38 removed
24 unmodified lines25262728293031323329303132333435363738394041424344454647353637383940414243444546474849495051525354555657585152535455565758596061626364656667686962636465666768697071727374757374757677787924 unmodified lines// notificationsapp.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'; // 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 = '<ul>', flash = req.flash(); Object.keys(flash).forEach(function(type){ flash[type].forEach(function(msg){ msgs += '<li class="' + type + '">' + msg + '</li>'; }); // 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 = '<ul>', flash = req.flash(); Object.keys(flash).forEach(function(type){ flash[type].forEach(function(msg){ msgs += '<li class="' + type + '">' + msg + '</li>'; }); }); msgs += '</ul>'; msgs += '</ul>';
// If we have a name, we are updating, // so add the hidden _method input res.send(msgs + '<form method="post">' + (name ? '<input type="hidden" value="put" name="_method" />' : '') + 'Name: <input type="text" name="name" value="' + name + '" />' + '<input type="submit" value="' + label + '" />' + '</form>'); // If we have a name, we are updating, // so add the hidden _method input res.send(msgs + '<form method="post">' + (name ? '<input type="hidden" value="put" name="_method" />' : '') + 'Name: <input type="text" name="name" value="' + name + '" />' + '<input type="submit" value="' + label + '" />' + '</form>');});
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('/'); } 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); // Typically here we would update a resource req.flash('info', 'Updated ' + req.body.name); res.redirect('/?name=' + req.body.name);});
app.listen(3000);console.log('Express app started on port 3000');No newline at end of fileNo newline at end of file