Permalink
Browse files

implement "before" middleware for mvc example

  • Loading branch information...
1 parent 3c12757 commit 3c162ae030dc5555144f6aa8c153ac30e6bd27d6 @tj tj committed Apr 26, 2012
Showing with 35 additions and 22 deletions.
  1. +10 −10 examples/mvc/controllers/pet/index.js
  2. +17 −11 examples/mvc/controllers/user/index.js
  3. +8 −1 examples/mvc/lib/boot.js
View
20 examples/mvc/controllers/pet/index.js
@@ -3,24 +3,24 @@ var db = require('../../db');
exports.engine = 'jade';
-exports.show = function(req, res, next){
+exports.before = function(req, res, next){
var pet = db.pets[req.params.pet_id];
if (!pet) return next(new Error('Pet not found'));
- res.render('show', { pet: pet });
+ req.pet = pet;
+ next();
+};
+
+exports.show = function(req, res, next){
+ res.render('show', { pet: req.pet });
};
exports.edit = function(req, res, next){
- var pet = db.pets[req.params.pet_id];
- if (!pet) return next(new Error('Pet not found'));
- res.render('edit', { pet: pet });
+ res.render('edit', { pet: req.pet });
};
exports.update = function(req, res, next){
- var id = req.params.pet_id;
- var pet = db.pets[id];
var body = req.body;
- if (!pet) return next(new Error('Pet not found'));
- pet.name = body.user.name;
+ req.pet.name = body.user.name;
res.message('Information updated!');
- res.redirect('/pet/' + id);
+ res.redirect('/pet/' + req.pet.id);
};
View
28 examples/mvc/controllers/user/index.js
@@ -1,28 +1,34 @@
var db = require('../../db');
+exports.before = function(req, res, next){
+ var id = req.params.user_id;
+ if (!id) return next();
+ // pretend to query a database...
+ process.nextTick(function(){
+ req.user = db.users[id];
+ // cant find that user
+ if (!req.user) return next(new Error('User not found'));
+ // found it, move on to the routes
+ next();
+ });
+}
+
exports.list = function(req, res, next){
res.render('list', { users: db.users });
};
exports.edit = function(req, res, next){
- var user = db.users[req.params.user_id];
- if (!user) return next(new Error('User not found'));
- res.render('edit', { user: user });
+ res.render('edit', { user: req.user });
};
exports.show = function(req, res, next){
- var user = db.users[req.params.user_id];
- if (!user) return next(new Error('User not found'));
- res.render('show', { user: user });
+ res.render('show', { user: req.user });
};
exports.update = function(req, res, next){
- var id = req.params.user_id;
- var user = db.users[id];
var body = req.body;
- if (!user) return next(new Error('User not found'));
- user.name = body.user.name;
+ req.user.name = body.user.name;
res.message('Information updated!');
- res.redirect('/user/' + id);
+ res.redirect('/user/' + req.user.id);
};
View
9 examples/mvc/lib/boot.js
@@ -18,11 +18,18 @@ module.exports = function(parent, options){
if (obj.engine) app.set('view engine', obj.engine);
app.set('views', __dirname + '/../controllers/' + name + '/views');
+ // before middleware support
+ if (obj.before) {
+ path = '/' + name + '/:' + name + '_id*';
+ app.all(path, obj.before);
+ verbose && console.log(' ALL %s -> before', path);
+ }
+
// generate routes based
// on the exported methods
for (var key in obj) {
// "reserved" exports
- if ('name' == key || 'prefix' == key || 'engine' == key) continue;
+ if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) continue;
// route exports
switch (key) {
case 'show':

0 comments on commit 3c162ae

Please sign in to comment.