2016-04-24 2 views
0

У меня есть следующая функция находится в сг-route.js, что гарантирует, что проверка подлинности пользователя перед началом работы и отображает его имяПочему мое приложение не достигает console.log?

module.exports = function (app, passport) { 

    // ===================================== 
    // HOME PAGE (with login links) ======== 
    // ===================================== 
    app.get('/', function (req, res) { 
     res.render('index.ejs'); // load the index.ejs file 
    }); 

    // ===================================== 
    // LOGIN =============================== 
    // ===================================== 
    // show the login form 
    app.route('/login') 
     .get(function (req, res) { 

      // render the page and pass in any flash data if it exists 
      res.render('login.ejs', { 
       message: req.flash('loginMessage') 
      }); 
     }) 

     // process the login form 
     .post(passport.authenticate('local-login', { 
       successRedirect: '/home', // redirect to the secure profile section 
       failureRedirect: '/', // redirect back to the signup page if there is an error 
       failureFlash: true // allow flash messages 
      }), 
      function (req, res) { 
       console.log("hello"); 


       if (req.body.remember) { 
        req.session.cookie.maxAge = 1000 * 60 * 3; 
       } else { 
        req.session.cookie.expires = false; 
       } 
       res.redirect('/'); 
      }); 

    // ===================================== 
    // SIGNUP ============================== 
    // ===================================== 
    // show the signup form 
    app.get('/signup', function (req, res) { 
     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', {message: req.flash('signupMessage')}); 
    }); 

    // process the signup form 
    app.post('/signup', passport.authenticate('local-signup', { 
     successRedirect: '/home', // redirect to the secure home section 
     failureRedirect: '/', // redirect back to the signup page if there is an error 
     failureFlash: true // allow flash messages 
    })); 

    // ===================================== 
    // Home SECTION ========================= 
    // ===================================== 
    // we will want this protected so you have to be logged in to visit 
    // we will use route middleware to verify this (the isLoggedIn function) 
    app.get('/home', isLoggedIn, function (req, res) { 
     res.render('home.ejs', { 
      title: 'C', 
      user: req.user // get the user out of session and pass to template 
     }); 
    }); 

    // ===================================== 
    // LOGOUT ============================== 
    // ===================================== 
    app.get('/logout', function (req, res) { 
     req.logout(); 
     res.redirect('/'); 
    }); 
}; 

Я называю этот маршрут из следующего модуля, расположенного по адресу home.js:

var express = require('express'); 
var router = express.Router(); 
var url = require('url'); 
var passport = require('passport'); 


    module.exports = function (app) { 
     require('../app/cr-route')(app, passport); 
     app.get('/home', function (req, res, next) { 
      var queryData = url.parse(req.url, true).query; 
      console.log('im in'); //not displaying 

     }); 
     return router; 
    }; 

тогда я звоню этот модуль из app.js файла, выполнив следующие действия:

require('./routes/home')(app); 

Но я ВГА e чувство, что хотя я могу успешно загрузить home.js, он все равно не получает доступ к методу get внутри него.

Как исправить эту проблему?

ответ

1

У вас есть тот же маршрут, /home, определенный в cr-route.js и home.js. Я предполагаю, что один из home.js никогда не вызывается, потому что он уже обрабатывается в cr-route.js

Смежные вопросы