简体   繁体   中英

User Login is not working with node.js and mongoose

I am trying to user login with node.js and 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.

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>

You specify in your router that login page can only be accessed with POST query. But then you redirect your user to /login page, you make a GET query, thats why you get 404.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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