我从快速和节点的基本样本开始。我也能够用mongoDB进行设置。现在问题出现了,因为我试图将代码重构为不同的模块,例如索引,登录和注册。我已经将不同的机制放入不同的文件中并添加了相同的路由但它根本就没有拿起那些文件(除了索引页面之外的任何东西)。
我附上了app.js的源代码。
var config = require('./config');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var routes = require('./routes/index');
var session=require('express-session');
var login = require('./routes/login');
var register = require('./routes/register');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//session and passport initialization
app.use(session({
secret: 'quifers',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
// Define the routes here
app.use('/', routes);
app.use('/login',login);
app.use('/register',register);
var Account = require('./models/account');
passport.use(new LocalStrategy(Account.authenticate()));
passport.serializeUser(Account.serializeUser());
passport.deserializeUser(Account.deserializeUser());
// mongoose
//mongoose.createConnection(config.db.URL);
mongoose.connect(config.db.URL);
var db = mongoose.connection;
db.on('error', function () {
console.error('database connection error:');
process.exit(1);
});
db.once('open', function (callback) {
debugger;
// yay!
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development / test error handler
// will print stacktrace
if (config.env.development || config.env.test) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: (config.env.development || config.env.test) ? err : {}
});
});
}
module.exports = app;
views文件夹中的我的login.js如下所示:
var passport = require('passport');
var Account = require('../models/account');
var express = require('express');
var router = express.Router();
router.get('/login', function(req, res) {
res.render('login', { user : req.user });
});
router.post('/login', passport.authenticate('local'), function(req, res) {
res.redirect('/');
});
module.exports=router;
但是当我去登录时,它会重新加载他的页面并重定向到相同的/登录路线并说没有找到。
我是新手表达,任何帮助都将不胜感激。谢谢
答案 0 :(得分:0)
它之所以不起作用的原因是因为您尝试访问的路线不存在。
login.js安装在' / login'下,但是在login.js文件中,您再次重新声明/登录,因此您最终得到的路径如下所示: 的 /登录/登录强>
要使其正常工作,请更换" / login" by" /"。
您的代码应如下所示:
var passport = require('passport');
var Account = require('../models/account');
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.render('login', { user : req.user });
});
router.post('/', passport.authenticate('local'), function(req, res) {
res.redirect('/');
});
module.exports=router;