我在Node上使用了Express .hbs模板.我使用护照来验证特定用户.我在MongoDB中使用的数据库.
这是我的注册路线:
var express = require('express'); var router = express.Router(); var passport = require('passport'); var userServices = require('../services/user-services'); router.get('/', function(req, res, next) { var vm = { title: 'Join this web', }; res.render('signup', vm); }); router.post('/', function(req, res, next) { userServices.addUser(req.body, function(err){ if(err){ var vm = { title: 'Create an account', input: req.body, error: err }; delete vm.input.password; return res.render('signup', vm); } req.login(req.body, function(err) { res.redirect('/profile'); }); }); }); router.post('/login', passport.authenticate('local'), function(req, res, next){ res.redirect('/profile'); }); module.exports = router;
这是我的登录路线:
var express = require('express'); var router = express.Router(); router.get('/', function(req, res, next) { if (req.user) { return res.redirect('/profile'); } res.render('login', { title: 'Login' }); }); module.exports = router;
我在passport-congig文件中定义了护照的配置:
module.exports=function(){ var passport = require('passport'); var passportLocal = require('passport-local'); var userServices = require('../services/user-services'); passport.use(new passportLocal.Strategy({usernameField: 'email'}, function(email, password, next) { userServices.findUser(email, function(err, user){ if(err){ return next(err); } if(!user||user.password!==password){ return next(null, null); } next(null, user); }); })); passport.serializeUser(function(user, next){ next(null, user.email); }); passport.deserializeUser(function(user, next){ userServices.findUser(email, function(err, user){ next(err, user); }); }); };
另外,这是我的app.js与护照相关的代码:
var passportConfig = require('./auth/passport-config'); passportConfig(); var app = express(); app.use(expressSession({ secret:'trawel man', saveUninitialized: false, resave: false })); app.use(passport.initialize()); app.use(passport.session());
这是我收到的错误的堆栈跟踪:
Error: Not Found at C:\Users\James\MEAN\app.js:56:15 at Layer.handle [as handle_request] (C:\Users\James\MEAN\node_modules\express\lib\router\layer.js:82:5) at trim_prefix (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:302:13) at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:270:7 at Function.proto.process_params (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:321:12) at next (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:261:10) at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:603:15 at next (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:246:14) at Function.proto.handle (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:166:3) at router (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:35:12) at Layer.handle [as handle_request] (C:\Users\James\MEAN\node_modules\express\lib\router\layer.js:82:5) at trim_prefix (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:302:13) at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:270:7 at Function.proto.process_params (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:321:12) at next (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:261:10) at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:603:15
这是我在服务器控制台上得到的:
POST /login 404 32.453 ms - 2847
我不知道为什么这不起作用.我是Node的新手.有人帮助我.