传递参数来表示路由中的中间件

我正在构build一个nodejs + express RESTful服务器,并试图利用中间件来简化特定操作的授权。

我想实现的是将parameter passing给我的授权中间件function。 我想知道在路由中是否完全可以做到这一点,或者如果我必须提取中间件function中的参数。 我希望避免这种行为,因为我已经*哼*不完全一致的我的url参数名称。

我想要做的是这样的:

router.get( '/:productId', auth.can_get_this_product(productId), // Pass the productId here controller.perform_action_that_requires_authorization ); 

但这是不可能的。 因为我有其他路线的参数名称可能不相同(即: router.get(/:p_id, auth.can_get_thi... )。我意识到我应该回去,并确保我的参数名称是一致无处不在,并使用req.param('productId')检索中间件中的参数,但我很好奇它是否可能。

那么,我想你可以传递参数哈希键,然后使用它。

 router.get( '/:productId', auth.can_get_this_product('productId'), // Pass the productId *KEY* here controller.perform_action_that_requires_authorization ); //.... function can_get_this_product(productIdKey) { var productId = req.params[productIdKey]; //.... } 

当然,我们都知道你应该硬着头皮重构这些名字。