Skip to content

Commit

Permalink
improve before hook in mvc example
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 6, 2014
1 parent 3db6dd7 commit 4279e6e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion examples/mvc/controllers/pet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var db = require('../../db');

exports.before = function(req, res, next){
var pet = db.pets[req.params.pet_id];
if (!pet) return next(new Error('Pet not found'));
if (!pet) return next('route');
req.pet = pet;
next();
};
Expand Down
2 changes: 1 addition & 1 deletion examples/mvc/controllers/user-pet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.create = 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'));
if (!user) return next('route');
var pet = { name: body.pet.name };
pet.id = db.pets.push(pet) - 1;
user.pets.push(pet);
Expand Down
2 changes: 1 addition & 1 deletion examples/mvc/controllers/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.before = function(req, res, next){
process.nextTick(function(){
req.user = db.users[id];
// cant find that user
if (!req.user) return next(new Error('User not found'));
if (!req.user) return next('route');
// found it, move on to the routes
next();
});
Expand Down
9 changes: 1 addition & 8 deletions examples/mvc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,9 @@ app.use(function(req, res, next){
// load controllers
require('./lib/boot')(app, { verbose: !module.parent });

// assume "not found" in the error msgs
// is a 404. this is somewhat silly, but
// valid, you can do whatever you like, set
// properties, use instanceof etc.
app.use(function(err, req, res, next){
// treat as 404
if (~err.message.indexOf('not found')) return next();

// log it
console.error(err.stack);
if (!module.parent) console.error(err.stack);

// error page
res.status(500).render('5xx');
Expand Down
27 changes: 14 additions & 13 deletions examples/mvc/lib/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,14 @@ module.exports = function(parent, options){
var name = obj.name || name;
var prefix = obj.prefix || '';
var app = express();
var handler;
var method;
var path;

// allow specifying the view engine
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);
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) {
Expand Down Expand Up @@ -62,15 +53,25 @@ module.exports = function(parent, options){
path = '/';
break;
default:
/* istanbul ignore next */
throw new Error('unrecognized route: ' + name + '.' + key);
}

// setup
handler = obj[key];
path = prefix + path;
app[method](path, obj[key]);
verbose && console.log(' %s %s -> %s', method.toUpperCase(), path, key);

// before middleware support
if (obj.before) {
app[method](path, obj.before, handler);
verbose && console.log(' %s %s -> before -> %s', method.toUpperCase(), path, key);
} else {
app[method](path, obj[key]);
verbose && console.log(' %s %s -> %s', method.toUpperCase(), path, key);
}
}

// mount the app
parent.use(app);
});
};
};
7 changes: 7 additions & 0 deletions test/acceptance/mvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ describe('mvc', function(){
})

describe('PUT /user/:id', function(){
it('should 500 on error', function(done){
request(app)
.put('/user/1')
.send({})
.expect(500, done)
})

it('should update the user', function(done){
request(app)
.put('/user/1')
Expand Down

0 comments on commit 4279e6e

Please sign in to comment.