如何在函数内的node.js中返回一个导入的模块?

我是一个创build一个node.js单页面的应用程序。 如果用户通过validation,我想在主页中显示特定的视图。 为此,我创build了一个函数来检查用户是否是auth。 它工作正常。

但是,当我想要返回一个特定的视图时,我会遇到一些问题。

我尝试了几种不同的方法,但是我不能返回任何视图。

意见

const notAuth = require('../view1') const isAuth = require('../view2') 

这是我第一次尝试:

 const home = function (ctx) { if (ctx.auth) { return isAuth } else { return notAuth } } module.exports = home 

然后,我试图只使用module.exports

 module.exports = function home (ctx, next) { if (ctx.auth) { return isAuth } else { return notAuth } next() } 

最后,我试了一下:

 const authenticated = function (ctx) { if (ctx.auth) { return isAuth } else { return notAuth } } module.exports = function home (ctx, next) { return authenticated(ctx) next() } 

注意:

我需要一个特定视图的每个模块,如果我使用例如:

 module.exports = notAuth 

我怎样才能在一个函数中返回一个特定的导入模块?

也许你需要将上下文实际传递给目标路由

 const home = function (ctx, next) { if (ctx.auth) { return isAuth(ctx, next) } else { return notAuth(ctx, next) } }