app.param中间件用于对URL中的参数进行获取、加工、输出,提供公有逻辑,以达到代码重构的目的。
以下示例采取三个步骤对代码进行重构,app.param中间件的作用非常明显:
- 不使用中间件
- 使用自定义中间件
- 使用app.param中间件
不使用中间件
1 var users = {
2 'azat': {
3 email: 'hi@azat.co',
4 website: 'http://azat.co',
5 blog: 'http://webapplog.com'
6 }
7 };
8
9 var findUserByUsername = function (username, callback) {
if (!users[username]) {
return callback(new Error('No user matching ' + username));
}
return callback(null, users[username]);
}
app.get('/v1/users/:username', function (request, response, next) {
var username = request.params.username;
findUserByUsername(username, function (error, user) {
if (error) return next(error);
return response.render('user', user);
});
});
app.get('/v1/admin/:username', function (request, response, next) {
var username = request.params.username;
findUserByUsername(username, function (error, user) {
if (error)return next(error);
return response.render('admin', user);
});
});
使用自定义中间件
1 var findUserByUsernameMiddleware = function (request, response, next) {
2 if (request.params.username) {
3 console.log('Username param was detected: ' + request.params.username);
4 findUserByUsername(request.params.username, function (error, user) {
5 if (error)return next(error);
6 request.user = user;
7 return next();
8 })
9 } else {
return next();
}
}
app.get('/v2/users/:username',
findUserByUsernameMiddleware,
function (request, response, next) {
return response.render('user', request.user);
});
app.get('/v2/admin/:username',
findUserByUsernameMiddleware,
function (request, response, next) {
return response.render('admin', request.user);
});
2 if (request.params.username) {
3 console.log('Username param was detected: ' + request.params.username);
4 findUserByUsername(request.params.username, function (error, user) {
5 if (error)return next(error);
6 request.user = user;
7 return next();
8 })
9 } else {
return next();
}
}
app.get('/v2/users/:username',
findUserByUsernameMiddleware,
function (request, response, next) {
return response.render('user', request.user);
});
app.get('/v2/admin/:username',
findUserByUsernameMiddleware,
function (request, response, next) {
return response.render('admin', request.user);
});
使用app.param中间件
1 app.param('v3Username',function(request,response,next,username){
2 console.log('Username param was detected: '+username);
3 findUserByUsername(username,function(error,user){
4 if (error)return next(error);
5 request.user = user;
6 return next();
7 });
8 });
9
app.get('/v3/users/:v3Username',
function (request, response, next) {
return response.render('user', request.user);
});
app.get('/v3/admin/:v3Username',
function (request, response, next) {
return response.render('admin', request.user);
});
2 console.log('Username param was detected: '+username);
3 findUserByUsername(username,function(error,user){
4 if (error)return next(error);
5 request.user = user;
6 return next();
7 });
8 });
9
app.get('/v3/users/:v3Username',
function (request, response, next) {
return response.render('user', request.user);
});
app.get('/v3/admin/:v3Username',
function (request, response, next) {
return response.render('admin', request.user);
});
小结
- app.param中间件起到AOP的作用,对含有指定参数的请求进行统一拦截处理,便于集中维护管理;
- app.param中间件简化了后面路由处理时的写法,无须再次显式调用中间件来处理,更简约。