1.設計路由
2.新增頁面
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Welcome <%= user.username %>, 歡迎登陸!!</h1>
<a claa="btn" href="/logout">退出</a>
</body>
</html>
login.ejs<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<form action="login" method="post">
用戶名:<input type="text" name="username">
密碼:<input type="password" name="pwd">
<input type="submit" value="登陸">
</form>
</body>
</html>
OK 我們需要的頁面已經建好了!!!!!3.增加路由配置
router.get('/login', function (req,res,next){
res.render('login', {title:'主頁' })
})
router.post('/login', function (req,res,next){
var user={
username:'admin',
pwd:123456
}
if(req.body.username==user.username && req.body.pwd==user.pwd){
res.redirect('home')
}
res.redirect('login');
})
router.get('/home', function (req,res,next){
var user={
username:'admin',
pwd:123456
}
res.render('home', { title: 'home',user: user })
})
router.get('/logout', function(req, res, next) {
res.redirect('login');
});
4.啓動項目查看效果
我們的項目已經寫好了。快去試試吧。5.頁面模塊化
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
foot.ejs </body>
</html>
修改home.ejs頁面爲<% include head%>
<h1>Welcome <%= user.username %>, 歡迎登陸!!</h1>
<a claa="btn" href="/logout">退出</a>
<% include foot %>
是不是簡潔很多恩!!!!