繁体   English   中英

用户登录不适用于node.js和mongoose

[英]User Login is not working with node.js and mongoose

我正在尝试使用node.js和mongoose登录。 用户注册成功运行,并且数据正在mongodb集合中插入,但是当我尝试使用注册的电子邮件和密码登录时,页面显示404错误。

login.js

var express = require('express');
var router = express.Router();


/* GET users listing. */
router.get('/', function(req, res, next) {
  res.render('login', { title: 'Express' });
});

router.post('/login', function(req, res, next){
user.findOne({email:req.body.email}, function(err, user){
  if(!user){
    res.render('login',{title:'Invalid'});
  }
  else{
    if(req.body.password===user.password){
      res.render('/dashboard',{title:'Success Fully login'});
    }
    else{
      res.render('login',{title:'invalide password'});
    }
  }
});
});

module.exports = router;

login.ejs

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>

  <div style="margin:0 auto ; width:50%; padding:20px">
    <form action="/login"  method="post">
    <label>Email</label>
    <input type="text" name="email">
      <br /> <br /> <br /> <br />
    <label>Pass</label>
    <input type="text" name="password">
      <br /> <br /> <br /> <br />
    <button>Login</button>
      </form>
    </div>
  </body>
</html>

您在路由器中指定只能使用POST查询访问登录页面。 但是随后您将用户重定向到/login页面,进行GET查询,这就是为什么得到404的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM