Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Route.js有多个路由文件,无法使用路由器get/post功能中的其他功能_Javascript_Node.js_Express_Routes - Fatal编程技术网

Javascript Route.js有多个路由文件,无法使用路由器get/post功能中的其他功能

Javascript Route.js有多个路由文件,无法使用路由器get/post功能中的其他功能,javascript,node.js,express,routes,Javascript,Node.js,Express,Routes,我已经成功地将Route.js分解到多个控制器,但是我不知道为什么我不能使用附加功能阻止查看页面的权限 // route.js module.exports = function(app, passport) { app.use('/profile', require('./controllers/profileController')); } 为什么我不能在profileController上执行此操作 router.get('/', p

我已经成功地将Route.js分解到多个控制器,但是我不知道为什么我不能使用附加功能阻止查看页面的权限

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

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

    }

为什么我不能在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中导出函数

// 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
   }

你能发布你的整个permissions.js文件吗?当然,在一分钟内重新编辑。完成。检查我的答案:谢谢!另外一个问题:我需要导出它的唯一原因是因为它是一个回调函数,对吗?当这两个函数都在profileController.js上时,我不需要这样做。不,你需要导出它们,因为它们在不同的文件中:PWas即将编辑我的问题来回答我自己,你完全正确。谢谢你的帮助:)
    // 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
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
   }