当前位置:  开发笔记 > 编程语言 > 正文

节点JS:登录不工作

如何解决《节点JS:登录不工作》经验,为你挑选了0个好方法。

我在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的新手.有人帮助我.

推荐阅读
跟我搞对象吧
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有