Skip to content
Myr

app.js

This file

Path

examples/form/app.js

81 lines · 2.2 KiB · JavaScript

Last changed by

fixing more examplesTj Holowaychukbefore adoption66a6214

History of this file

Take it

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

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

line 1why, one step back
1

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 2–4why, one step back
2// Expose modules in ./support for demo purposes
3require.paths.unshift(__dirname + '/../../support');4

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

lines 5–8why, one step back
5/**
6 * Module dependencies.7 */8

fixing more examplesTj Holowaychukbefore adoption66a6214

line 9why, one step back
9var express = require('../../lib/express');

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

lines 10–15why, one step back
10
11var app = express.createServer();1213// Here we use the bodyDecoder middleware14// to parse urlencoded request bodies15// which populates req.body

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

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

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

lines 17–23why, one step back
17
18// The methodOverride middleware allows us19// to set a hidden input of _method to an arbitrary20// HTTP method to support app.put(), app.del() etc21app.use(express.methodOverride());2223// Required by session

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

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

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

lines 25–27why, one step back
25
26// Required by req.flash() for persistent27// notifications

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

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

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

lines 29–81why, one step back
29
30app.get('/', function(req, res){31 // get ?name=foo32 var name = req.param('name') || '';33 34 // Switch the button label based if we have a name35 var label = name ? 'Update' : 'Save';3637 // Buffer all flash messages.38 // Typically this would all be done in a template39 // however for illustration purposes we iterate40 // here.41 42 // The messages in req.flash() persist until called,43 // at which time they are flushed from the session44 var msgs = '<ul>',45 flash = req.flash();46 Object.keys(flash).forEach(function(type){47 flash[type].forEach(function(msg){48 msgs += '<li class="' + type + '">' + msg + '</li>';49 });50 });51 msgs += '</ul>';5253 // If we have a name, we are updating,54 // so add the hidden _method input55 res.send(msgs56 + '<form method="post">'57 + (name ? '<input type="hidden" value="put" name="_method" />' : '')58 + 'Name: <input type="text" name="name" value="' + name + '" />'59 + '<input type="submit" value="' + label + '" />'60 + '</form>');61});6263app.post('/', function(req, res){64 if (req.body.name) {65 // Typically here we would create a resource66 req.flash('info', 'Saved ' + req.body.name);67 res.redirect('/?name=' + req.body.name);68 } else {69 req.flash('error', 'Error: name required');70 res.redirect('/');71 }72});7374app.put('/', function(req, res){75 // Typically here we would update a resource76 req.flash('info', 'Updated ' + req.body.name);77 res.redirect('/?name=' + req.body.name);78});7980app.listen(3000);81console.log('Express app started on port 3000');