我试图为不同的参数使用两个不同的app.param,但似乎有可能。当服务器检查第一个参数时,我会按顺序检查第二个参数,即使它正在检查第二个app.param中的第二个参数,它也会使第一个参数无效。
app.param('chatroom', function(req, res, next, chatroom) {
Chatroom.findOne({ 'chat.roomname' : chatroom }, function(err, chatroom) {
if(err)
throw err;
if(!chatroom)
return next();
req.chatroom = chatroom;
next();
});
});
app.get('/:chatroom', function(req, res) {
Profile.findOne({ 'pic.username' : req.user.local.username}, function(err, profilepic) {
if(err)
throw err;
res.render('pages/chatroom', {
chatroom : req.chatroom,
profilepic : profilepic,
user : req.user
});
});
});
app.param('username', function(req, res, next, username) {
User.findOne({ 'local.username' : username }, function(err, user) {
if(err)
throw err;
if(!user){
return res.sendStatus(404);
}
req. profile = user;
next();
});
});
app.get('/:username', function(req, res) {
async.parallel([
function(callback) {
Profile.findOne({ 'pic.username' : req. profile.local.username }, function(err, profilepic) {
if(err)
throw err;
callback(null, profilepic);
});
}, function(callback) {
Buddy.find({ 'bud.username' : req. profile.local.username }, function(err, buddy) {
if(err)
throw err;
callback(null, buddy);
});
}, function(callback) {
Buddy.count({ 'bud.username' : req. profile.local.username}, function(err, buddycount) {
if(err)
throw err;
callback(null, buddycount);
});
}, function(callback){
Userpost.find({ 'post.postuser' : req. profile.local.username }).sort({ 'post.date' : 1 }).exec(function(err, userpost) {
if(err)
throw err;
callback(null, userpost);
});
}, function(callback) {
Userpost.count({ 'post.postuser' : req. profile.local.username }, function(err,postcount) {
if(err)
throw err;
callback(null, postcount);
});
}], function(err, results){
if(err)
throw err;
var profilepic = results[0];
var buddy = results[1];
var buddycount = results[2];
var userpost = results[3];
var postcount = results[4];
res.render('pages/visitprofile', {
user : req. profile,
profilepic : profilepic,
buddy : buddy,
buddycount : buddycount,
userpost : userpost,
postcount : postcount
});
}
);
});
所以我可以同时让两个参数在服务器上运行。我重新排序它们,一个与另一个无法正常工作。也许我可以使next()函数跳过其他参数一些如何?