留言板功能
app.js
// app.jsconst express = require('express');const moment = require('moment');const path = require('path');const morgan = require('morgan');const bodyParser = require('body-parser');const app = express();// 注册模版引擎app.set('views', path.join(__dirname, 'views'));app.set('view engine', 'ejs');// 设置post接收中间件,所有提交的数据都会保留在req.body// extended: false:表示使用系统模块querystring来处理,也是官方推荐的// extended: true:表示使用第三方模块qs来处理app.use(bodyParser.urlencoded({extended:false}));// 设置全局变量,服务器整个生命周期let entries = [];app.locals.entries = entries;app.get('/', (req, res) => { res.render('index.ejs');});app.get('/new', (req, res) => { res.render('new.ejs');});// 数据提交app.post('/new',(req,res)=>{ console.log(req.body); if(!req.body.title || !req.body.content){ res.status(400).send('留言必须要有标题和内容'); return ''; } // 保留用户的留言 entries.push({ title: req.body.title, content: req.body.content, published: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') }); console.log(entries); // 回到主页面,重定向 res.redirect('/');});// 渲染404页面app.use((req, res) => { res.status(404).render('404.ejs');});app.listen(3000, () => { console.log('服务器启动!');});
view
index.ejs
<%- include('header.ejs') %><h2 class="text-center mt-5">GoBig-留言板</h2><div class="text-center mt-3 mb-5"> <a href="/new" class="btn btn-sm btn-danger">我要留言</a></div><div class="container"> <div class="row mt-3"> <% if(entries.length){ %> <% entries.forEach(function (entry) { %> <div class="card mt-2" style="width: 100%;"> <div class="card-body"> <h5 class="card-title"><%= entry.title %></h5> <h6 class="card-subtitle mb-2 text-muted"><%= entry.published %></h6> <p class="card-text"><%= entry.content %></p> </div> </div> <% }) %> <% } %> </div></div><script></script><%- include('footer.ejs') %>
new.ejs
// new.ejs<%- include('header.ejs') %><div class="container"> <div class="row mt-5"> <div class="col"> <h3 class="text-center">请写下您的留言</h3> </div> </div> <div class="row"> <div class="col"> <form method="post" role="form"> <div class="form-group"> <label for="title">留言标题</label> <input type="text" class="form-control " id="title" name="title" placeholder="请输入标题..."> </div> <div class="form-group"> <label for="title">留言内容</label> <textarea rows="5" class="form-control " id="content" name="content" placeholder="请输入留言内容..."></textarea> </div> <div class="form-group"> <input type="submit" class="btn btn-info float-right" value="提交留言"> </div> </form> </div> </div></div><%- include('footer.ejs') %>
404.js
<%- include('header.ejs') %><div class="position-relative" style="height: 1000px;"> <div class="h-75 "> <h2 class="text-center text-danger bg-primary m-auto" style="height:100px;line-height:100px;position: absolute;top: 0;bottom: 0;right: 0;left: 0">404这个页面找不到啦找不到啦</h2> </div></div><%- include('footer.ejs') %>
header.js
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css"> <title>GoBig</title></head><body>
footer.js
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"></script>
</body>
</html>
package.json
{
"name": "demo",
"version": "1.0.0",
"description": "",
"dependencies": {
"ejs": "^3.1.3",
"express": "^4.17.1",
"body-parser": "^1.19.0",
"moment": "^2.27.0",
"morgan": "^1.10.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}