我在cr-route.js中有以下功能,确保在继续操作之前对用户进行身份验证并显示他的名字
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);
但我有一种感觉,虽然我能够成功加载home.js,但它仍然没有访问其中的get方法。
如何解决此问题?
答案 0 :(得分:1)
您拥有/home
和cr-route.js
中定义的相同路线home.js
。我假设home.js
中的那个永远不会被调用,因为它已经在cr-route.js