Skip to content
Myr

app.js

This file

Path

examples/form/app.js

79 lines · 2.1 KiB · JavaScript

Last changed by

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

History of this file

Take it

The walk stopped after 200 commits: lines older than ce2a18f point at it.

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 1–13why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('./../../lib/express'),7 sys = require('sys');89var app = express.createServer();1011// Here we use the bodyDecoder middleware12// to parse urlencoded request bodies13// which populates req.body

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

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

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 15–21why, one step back
15
16// The methodOverride middleware allows us17// to set a hidden input of _method to an arbitrary18// HTTP method to support app.put(), app.del() etc19app.use(express.methodOverride());2021// Required by session

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

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

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

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

Added secret to session middleware used in examples and generated appTj Holowaychukbefore adoption7687299

line 26why, one step back
26app.use(express.session({ secret: 'keyboard cat' }));

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 27–28why, one step back
27
28app.get('/', function(req, res){

refactored form exampleTj Holowaychukbefore adoptionc1cfd4a

lines 29–33why, one step back
29 // get ?name=foo
30 var name = req.param('name') || '';31 32 // Switch the button label based if we have a name33 var label = name ? 'Update' : 'Save';

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 34why, one step back
34

refactored form exampleTj Holowaychukbefore adoptionc1cfd4a

lines 35–46why, 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>';

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 47why, one step back
47 });

refactored form exampleTj Holowaychukbefore adoptionc1cfd4a

lines 48–49why, one step back
48 });
49 msgs += '</ul>';

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 50why, one step back
50

refactored form exampleTj Holowaychukbefore adoptionc1cfd4a

lines 51–58why, one step back
51 // If we have a name, we are updating,
52 // so add the hidden _method input53 res.send(msgs54 + '<form method="post">'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>');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 59–61why, one step back
59});
6061app.post('/', function(req, res){

refactored form exampleTj Holowaychukbefore adoptionc1cfd4a

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 }

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 70–72why, one step back
70});
7172app.put('/', function(req, res){

refactored form exampleTj Holowaychukbefore adoptionc1cfd4a

lines 73–75why, one step back
73 // Typically here we would update a resource
74 req.flash('info', 'Updated ' + req.body.name);75 res.redirect('/?name=' + req.body.name);

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 76–79why, one step back
76});
7778app.listen(3000);79console.log('Express app started on port 3000');