【发布时间】:2016-08-17 16:22:06
【问题描述】:
我在 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方法。
我该如何解决这个问题?
【问题讨论】:
标签: javascript node.js express routes middleware