improve before hook in mvc example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 3db6dd7 · Browse files · 6 files·+25 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+25 added−24 removed
- tests:+7 added−0 removed
- other:+18 added−24 removed
Provenance
- Commit
4279e6e- Tree
9e58631- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
3db6dd7
6 files changed+25 added−24 removed
modified:examples/mvc/controllers/pet/index.js+1 added-1 removed
4 unmodified lines5678991011121314 unmodified lines4 unmodified linesvar 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();};
14 unmodified linesmodified:examples/mvc/controllers/user/index.js+1 added-1 removed
9 unmodified lines1011121314141516171819 unmodified lines9 unmodified lines // 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')); if (!req.user) return next('route'); // found it, move on to the routes next(); });};19 unmodified linesmodified:examples/mvc/controllers/user-pet/index.js+1 added-1 removed
9 unmodified lines101112131414151617182 unmodified lines9 unmodified linesexports.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); res.message('Added pet ' + body.pet.name);2 unmodified linesmodified:examples/mvc/lib/boot.js+14 added-13 removed
11 unmodified lines121314151617181920212223232425262728293031322425262724 unmodified lines525354555657585960616263646566676869706970717273747576767711 unmodified lines var obj = require('./../controllers/' + name); 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) { // "reserved" exports24 unmodified lines method = 'get'; path = '/'; break; default: /* istanbul ignore next */ throw new Error('unrecognized route: ' + name + '.' + key); }
// setup handler = obj[key]; path = prefix + path;
// 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); app[method](path, obj[key]); verbose && console.log(' %s %s -> %s', method.toUpperCase(), path, key); } }
// mount the app parent.use(app); });};No newline at end of file};modified:examples/mvc/index.js+1 added-8 removed
71 unmodified lines72 73 // load controllers74 require('./lib/boot')(app, { verbose: !module.parent });75 76-// assume "not found" in the error msgs77-// is a 404. this is somewhat silly, but78-// valid, you can do whatever you like, set79-// properties, use instanceof etc.76 app.use(function(err, req, res, next){81- // treat as 40482- if (~err.message.indexOf('not found')) return next();83-77 // log it85- console.error(err.stack);78+ if (!module.parent) console.error(err.stack);79 80 // error page81 res.status(500).render('5xx');82 });11 unmodified linesmodified:test/acceptance/mvc.js+7 added
90 unmodified lines91 })92 })93 94 describe('PUT /user/:id', function(){95+ it('should 500 on error', function(done){96+ request(app)97+ .put('/user/1')98+ .send({})99+ .expect(500, done)100+ })101+102 it('should update the user', function(done){103 request(app)104 .put('/user/1')105 .send({ user: { name: 'Tobo' }})24 unmodified lines