簡體   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