Implemented route middleware support

This commit is contained in:
Tj Holowaychuk 2010-10-07 03:30:39 -07:00
parent 8a845e2951
commit 1722e2c7a5
2 changed files with 17 additions and 1 deletions

View File

@ -71,11 +71,16 @@ function restrict(req, res, next) {
} }
} }
function accessLogger(req, res, next) {
console.log('/restricted accessed by %s', req.session.user.name);
next();
}
app.get('/', function(req, res){ app.get('/', function(req, res){
res.redirect('/login'); res.redirect('/login');
}); });
app.get('/restricted', restrict, function(req, res){ app.get('/restricted', restrict, accessLogger, function(req, res){
res.send('Wahoo! restricted area'); res.send('Wahoo! restricted area');
}); });

View File

@ -307,9 +307,20 @@ Server.prototype.configure = function(env, fn){
(function(method){ (function(method){
Server.prototype[method] = function(path, fn){ Server.prototype[method] = function(path, fn){
// Ensure router is mounted
if (!this.__usedRouter) { if (!this.__usedRouter) {
this.use(this.router); this.use(this.router);
} }
// Route specific middleware support
if (arguments.length > 2) {
for (var i = 1, len = arguments.length - 1; i < len; ++i) {
this[method](path, arguments[i]);
}
fn = arguments[arguments.length - 1];
}
// Generate the route
this.routes[method](path, fn); this.routes[method](path, fn);
return this; return this;
}; };