如何调用直接连接中间件?

我有一个像这样的快速路线:

app.get('/', auth.authOrDie, function(req, res) { res.send(); }); 

authOrDie函数是这样定义的(在我的auth.js模块中):

 exports.authOrDie = function(req, res, next) { if (req.isAuthenticated()) { return next(); } else { res.send(403); } }); 

现在,当用户没有通过身份validation,我想validationhttp请求是否有一个授权(基本)标题。 要做到这一点,我想使用伟大的连接中间件basicAuth() 。

如您所知,Express是build立在Connect之上的,所以我可以使用express.basicAuth

basicAuth通常是这样使用的:

 app.get('/', express.basicAuth(function(username, password) { // username && password verification... }), function(req, res) { res.send(); }); 

但是,我想在我的authOrDie函数中使用它:

 exports.authOrDie = function(req, res, next) { if (req.isAuthenticated()) { return next(); } else if { // express.basicAuth ??? ****** } else { res.send(403); } }); 

******如何使用好参数(req?res?next?…)来调用basicAuth函数。

谢谢。

调用express.basicAuth函数会返回中间件函数来调用,所以你可以像这样直接调用它:

 exports.authOrDie = function(req, res, next) { if (req.isAuthenticated()) { return next(); } else { return express.basicAuth(function(username, password) { // username && password verification... })(req, res, next); } });