用户登录不适用于节点.js和猫鼬

User Login is not working with node.js and mongoose

本文关键字:js 节点 登录 不适用 适用于 用户      更新时间:2023-09-26

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

登录.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;

登录.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的原因。