javascript - Route.js 多个路由文件并且无法在路由器 get/post 函数中使用附加功能

标签 javascript node.js express routes

我已经成功地将 Route.js 分解为多个 Controller ,但我不知道为什么我不能使用附加函数来阻止查看页面的权限。

    // route.js
    module.exports = function(app, passport) {

        app.use('/profile', require('./controllers/profileController'));

    }

.

    // profileController.js
    var express     = require('express')
        , router      = express.Router()
        , permissions = require('../utils/permissions.js')

    // =====================================
    // /profile
    // =====================================
    router.get('/', function(req, res) {
        res.render('Profile/index.ejs', {});
    }

    module.exports = router;

为什么我不能在 profileController 上执行此操作?

    router.get('/', permissions.isLoggedIn, function(req, res) {
        res.render('Profile/index.ejs', {});
    }

它抛出错误:

    Error: Route.get() requires callback functions but got a [object Undefined]
            at /home/ubuntu/workspace/node_modules/express/lib/router/route.js:171:15
            at Array.forEach (native)
            at Route.(anonymous function) [as get] (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:167:15)
            at Function.proto.(anonymous function) [as get] (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:380:19)
            at Object.<anonymous> (/home/ubuntu/workspace/app/controllers/profileController.js:10:10)
            at Module._compile (module.js:409:26)
            at Object.Module._extensions..js (module.js:416:10)
            at Module.load (module.js:343:32)
            at Function.Module._load (module.js:300:12)
            at Module.require (module.js:353:17)
            at require (internal/module.js:12:17)

其他信息:

    // permissions.js
    var helpers  = require('./helpers.js');

    // route middleware to make sure a user is logged in
    function isLoggedInAJAX(req, res, next) {

        // if user is authenticated in the session, carry on 
        if (req.isAuthenticated())
            return next();

        res.json(helpers.shootMessage(helpers.getNOK(), "You're not logged in!"));
    }

    // route middleware to make sure a user is logged in
    function isLoggedIn(req, res, next) {

        // if user is authenticated in the session, carry on 
        if (req.isAuthenticated())
            return next();

        //save the page
        req.session.returnTo = req.path;

        // if they aren't redirect them to the home page
        //res.redirect('/info');
        res.redirect('/auth/facebook');
    }

最佳答案

你的问题是你没有在permissions.js中导出你的函数

// permissions.js
var helpers  = require('./helpers.js');

// route middleware to make sure a user is logged in
function isLoggedInAJAX(req, res, next) {

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated())
        return next();

    res.json(helpers.shootMessage(helpers.getNOK(), "You're not logged in!"));
}

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated())
        return next();

    //save the page
    req.session.returnTo = req.path;

    // if they aren't redirect them to the home page
    //res.redirect('/info');
    res.redirect('/auth/facebook');
}

module.exports = {
     isLoggedInAJAX : isLoggedInAJAX,
     isLoggedIn : isLoggedIn
   }

关于javascript - Route.js 多个路由文件并且无法在路由器 get/post 函数中使用附加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36937068/

相关文章:

javascript - 在 user_timeline 上获取 403 Forbidden 但在任何其他 API 上都没有

JavaScript 对象文字长度 === 未定义?

node.js - 如何动态决定请求是否应该通过 Express 中的中间件

javascript - JQuery/AJAX Toggle 将 POST 切换到 php 文件,无需导航

node.js - Node 异步推送循环

node.js - fs.createWriteStream 错误 :ENOENT

node.js - Node Jsreport 语法错误 : Identifier 'err' has already been declared

node.js - 如何从 node.js 中的 url 下载 pdf 文件?

javascript - 如何从重新定位的 float div 折叠空白区域?

javascript - 如何使容器 div 仅水平溢出而不对其内部 div 使用 "display:inline-block"?