javascript - User Login is not working with node.js and mongoose -
i trying user login node.js , mongoose. user registration working , data inserting in mongodb collection, when try login registered email , password page show 404 error.
login.js
var express = require('express'); var router = express.router(); /* 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 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 router login page can accessed post query. redirect user /login
page, make query, thats why 404.
Comments
Post a Comment