Skip to content
Myr

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 file

Take 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 back
1
2/**3 * Module dependencies.4 */56var express = require('./../../lib/express'),7 sys = require('sys');8

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

lines 9–13why, one step back
9var app = express.createServer();
1011// Here we use the bodyDecoder middleware12// to parse urlencoded request bodies13// which populates req.body

Updated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f

line 14why, one step back
14app.use(express.bodyDecoder());

Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec

line 15why, one step back
15

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

lines 16–18why, one step back
16// The methodOverride middleware allows us
17// to set a hidden input of _method to an arbitrary18// HTTP method to support app.put(), app.del() etc

Updated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f

line 19why, one step back
19app.use(express.methodOverride());

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

line 20why, one step back
20

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

line 21why, one step back
21// Required by session

Updated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f

line 22why, one step back
22app.use(express.cookieDecoder());

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

line 23why, one step back
23

Cleaning up form exampleTj Holowaychukbefore adoption656e67e

lines 24–25why, one step back
24// Required by req.flash() for persistent
25// notifications

Updated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f

line 26why, one step back
26app.use(express.session());

Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec

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

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

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

Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec

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

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

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

Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec

lines 55–61why, one step back
55 + (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 back
62 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 back
70});
7172app.put('/', function(req, res){73 // Typically here we would update a resource

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

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

Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec

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