mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 00:19:48 +01:00
Implemented route middleware support
This commit is contained in:
parent
8a845e2951
commit
1722e2c7a5
|
|
@ -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){
|
||||
res.redirect('/login');
|
||||
});
|
||||
|
||||
app.get('/restricted', restrict, function(req, res){
|
||||
app.get('/restricted', restrict, accessLogger, function(req, res){
|
||||
res.send('Wahoo! restricted area');
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -307,9 +307,20 @@ Server.prototype.configure = function(env, fn){
|
|||
|
||||
(function(method){
|
||||
Server.prototype[method] = function(path, fn){
|
||||
// Ensure router is mounted
|
||||
if (!this.__usedRouter) {
|
||||
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);
|
||||
return this;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user