routes/index.js 渲染模板
/*渲染注册页 */
router.get('/regist',function(req,res,next){
res.render('regist',{});
});
//渲染登录页
router.get('/login',function(req,res,next){
res.render('login',{});
});
routes/users.js 注册接口
router.post("/regist",function(req,res,next){
var data={
username:req.body.username,
password:req.body.password1,
password2:req.body.password2
}
model.connect(function(db){
db.collection("users").insertOne(data,function(err,ret){
if(err){
console.log("注册失败");
res.redirect("/regist");
}else{
res.redirect("/login")
}
})
})
})
views /regist.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title>注册页</title>
<%- include head %>
</head>
<body>
<div class="form-box">
<form action="/users/regist" method="post">
<input type="text" name="username" value="" placeholder="请输入用户名">
<input type="password" name="password1" value="" placeholder="请输入密码">
<input type="password" name="password2" value="" placeholder="请确认密码">
<input type="submit" name="" value="注册">
</form>
<div>已有账号,<a href="/login">立即登陆</a>!</div>
</div>
</body>
</html>
views /login.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title>登陆</title>
<%- include head %>
</head>
<body>
<div class="form-box">
<form action="/users/login" method="post">
<input type="text" name="username" value="" placeholder="请输入用户名">
<input type="password" name="password1" value="" placeholder="请输入密码">
<input type="submit" name="" value="登陆">
</form>
<div>已有账号,<a href="/resgit">立即注册</a>!</div>
</div>
</body>
</html>
views/head.ejs
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/stylesheets/style.css"/>