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) {
10 if (!users[username]) {
11 return callback(new Error('No user matching ' + username));
12 }
13
14 return callback(null, users[username]);
15 }
16
17 app.get('/v1/users/:username', function (request, response, next) {
18 var username = request.params.username;
19 findUserByUsername(username, function (error, user) {
20 if (error) return next(error);
21 return response.render('user', user);
22 });
23 });
24
25 app.get('/v1/admin/:username', function (request, response, next) {
26 var username = request.params.username;
27 findUserByUsername(username, function (error, user) {
28 if (error)return next(error);
29 return response.render('admin', user);
30 });
31 });
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) {
10 if (!users[username]) {
11 return callback(new Error('No user matching ' + username));
12 }
13
14 return callback(null, users[username]);
15 }
16
17 app.get('/v1/users/:username', function (request, response, next) {
18 var username = request.params.username;
19 findUserByUsername(username, function (error, user) {
20 if (error) return next(error);
21 return response.render('user', user);
22 });
23 });
24
25 app.get('/v1/admin/:username', function (request, response, next) {
26 var username = request.params.username;
27 findUserByUsername(username, function (error, user) {
28 if (error)return next(error);
29 return response.render('admin', user);
30 });
31 });
使用自定义中间件
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 {
10 return next();
11 }
12 }
13
14 app.get('/v2/users/:username',
15 findUserByUsernameMiddleware,
16 function (request, response, next) {
17 return response.render('user', request.user);
18 });
19
20 app.get('/v2/admin/:username',
21 findUserByUsernameMiddleware,
22 function (request, response, next) {
23 return response.render('admin', request.user);
24 });
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 {
10 return next();
11 }
12 }
13
14 app.get('/v2/users/:username',
15 findUserByUsernameMiddleware,
16 function (request, response, next) {
17 return response.render('user', request.user);
18 });
19
20 app.get('/v2/admin/:username',
21 findUserByUsernameMiddleware,
22 function (request, response, next) {
23 return response.render('admin', request.user);
24 });
使用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
10 app.get('/v3/users/:v3Username',
11 function (request, response, next) {
12 return response.render('user', request.user);
13 });
14
15 app.get('/v3/admin/:v3Username',
16 function (request, response, next) {
17 return response.render('admin', request.user);
18 });
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
10 app.get('/v3/users/:v3Username',
11 function (request, response, next) {
12 return response.render('user', request.user);
13 });
14
15 app.get('/v3/admin/:v3Username',
16 function (request, response, next) {
17 return response.render('admin', request.user);
18 });
小结
- app.param中间件起到AOP的作用,对含有指定参数的请求进行统一拦截处理,便于集中维护管理;
- app.param中间件简化了后面路由处理时的写法,无须再次显式调用中间件来处理,更简约。