简体   繁体   English

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

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

I am trying to user login with node.js and mongoose. 我正在尝试使用node.js和mongoose登录。 User Registration is successfully working and data is inserting in mongodb collection, but when I try to login with registered email and password the page show 404 error. 用户注册成功运行,并且数据正在mongodb集合中插入,但是当我尝试使用注册的电子邮件和密码登录时,页面显示404错误。

login.js 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 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>

You specify in your router that login page can only be accessed with POST query. 您在路由器中指定只能使用POST查询访问登录页面。 But then you redirect your user to /login page, you make a GET query, thats why you get 404. 但是随后您将用户重定向到/login页面,进行GET查询,这就是为什么得到404的原因。

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

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