Skip to content
Myr

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 file

Take it

Added form exampleTj Holowaychukbefore adoption018d865

lines 1–9why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('./../../lib/express'),7 connect = require('connect'),8 sys = require('sys');9

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

lines 10–15why, one step back
10var 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 back
16

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

lines 17–20why, one step back
17// The methodOverride middleware allows us
18// 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 back
21

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

lines 22–23why, one step back
22// Required by session
23app.use(connect.cookieDecoder());

Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8

line 24why, one step back
24

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

lines 25–27why, one step back
25// Required by req.flash() for persistent
26// notifications27app.use(connect.session());

Added form exampleTj Holowaychukbefore adoption018d865

lines 28–35why, one step back
28
29app.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';35

Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8

lines 36–51why, one step back
36 // 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>';51

Added form exampleTj Holowaychukbefore adoption018d865

lines 52–53why, one step back
52 // If we have a name, we are updating,
53 // so add the hidden _method input

Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8

lines 54–55why, one step back
54 res.send(msgs
55 + '<form method="post">'

Added form exampleTj Holowaychukbefore adoption018d865

lines 56–62why, one step back
56 + (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 back
63 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 back
71});
7273app.put('/', function(req, res){74 // Typically here we would update a resource

Added req.flash() to form example. Closes #338Tj Holowaychukbefore adoptionc4278c8

line 75why, one step back
75 req.flash('info', 'Updated ' + req.body.name);

Added form exampleTj Holowaychukbefore adoption018d865

lines 76–79why, one step back
76 res.redirect('/?name=' + req.body.name);
77});7879app.listen(3000);