我是NodeJS和Express的新手,我正在尝试创建一个登录注册页面来理解基本概念。我的问题与this类似,但没有发布解决方案。我正在使用bootstrap modal登录。我做了什么来显示登录模式:
<tr>
<td>
<b>Full Name: </b>
</td>
<td>
<input type="text" name="full_name" placeholder="Full Name" required/>
</td>
</tr>
<div class="modal fade text-center" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
链接login
并在<a href="/login" data-toggle="modal" data-target="#myModal"> Login</a>
档
server.js
var reqPath = path.join(__dirname, '../', 'views/pages/userLoginPopup');
router.get('/', function(req, res) {
res.render(reqPath, {isvalid: true}); //isvalid variable is for displaying error
});
文件.ejs
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<%if(!isvalid){%>
<div class="alert alert-danger alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close" onclick="this.parentElement.style.display='none'">×</a>
<strong>Incorrect credentials!</strong>
</div>
<%}%>
<form action = "/login" method="POST">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user"></i>
</span>
<input placeholder="Name" class="form-control" required type = "text" name = "ccname"/>
</div> <br />
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i>
</span>
<input placeholder="ID" class="form-control" required type = "text" name = "ccid"/>
</div> <br />
<input type = "submit" value = "Login" class="btn btn-md btn-success btn-block">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
文件中检索表单数据并检入数据库以进行身份验证,如果用户已通过身份验证,则会重定向到server.js
页面,如果没有,则会使用其他值呈现当前页面home
旗帜。isvalid
我做错了什么有人可以帮忙吗?我知道我需要传递模态ID,其中需要加载模态(在加载初始登录模式时完成)但我不知道如何在提交表单时这样做。提前谢谢。
更新2:项目结构
router.post('/', function(req, res) {
var lname = req.body.ccname;
var lid = req.body.ccid;
service.loginUser(lname, lid, function (err, result, privilege) {
if(err){
res.render('./pages/error', {errorName: err});
}else {
if(result){
req.session.name = lname;
req.session.uid = lid;
req.session.privilege = privilege;
res.redirect('/'); //home.js
}else {
res.render(reqPath, {isvalid: false});
}
}
});
});