留言板功能

app.js

  1. // app.js
  2. const express = require('express');
  3. const moment = require('moment');
  4. const path = require('path');
  5. const morgan = require('morgan');
  6. const bodyParser = require('body-parser');
  7. const app = express();
  8. // 注册模版引擎
  9. app.set('views', path.join(__dirname, 'views'));
  10. app.set('view engine', 'ejs');
  11. // 设置post接收中间件,所有提交的数据都会保留在req.body
  12. // extended: false:表示使用系统模块querystring来处理,也是官方推荐的
  13. // extended: true:表示使用第三方模块qs来处理
  14. app.use(bodyParser.urlencoded({extended:false}));
  15. // 设置全局变量,服务器整个生命周期
  16. let entries = [];
  17. app.locals.entries = entries;
  18. app.get('/', (req, res) => {
  19. res.render('index.ejs');
  20. });
  21. app.get('/new', (req, res) => {
  22. res.render('new.ejs');
  23. });
  24. // 数据提交
  25. app.post('/new',(req,res)=>{
  26. console.log(req.body);
  27. if(!req.body.title || !req.body.content){
  28. res.status(400).send('留言必须要有标题和内容');
  29. return '';
  30. }
  31. // 保留用户的留言
  32. entries.push({
  33. title: req.body.title,
  34. content: req.body.content,
  35. published: moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  36. });
  37. console.log(entries);
  38. // 回到主页面,重定向
  39. res.redirect('/');
  40. });
  41. // 渲染404页面
  42. app.use((req, res) => {
  43. res.status(404).render('404.ejs');
  44. });
  45. app.listen(3000, () => {
  46. console.log('服务器启动!');
  47. });

view

index.ejs

  1. <%- include('header.ejs') %>
  2. <h2 class="text-center mt-5">GoBig-留言板</h2>
  3. <div class="text-center mt-3 mb-5">
  4. <a href="/new" class="btn btn-sm btn-danger">我要留言</a>
  5. </div>
  6. <div class="container">
  7. <div class="row mt-3">
  8. <% if(entries.length){ %>
  9. <% entries.forEach(function (entry) { %>
  10. <div class="card mt-2" style="width: 100%;">
  11. <div class="card-body">
  12. <h5 class="card-title"><%= entry.title %></h5>
  13. <h6 class="card-subtitle mb-2 text-muted"><%= entry.published %></h6>
  14. <p class="card-text"><%= entry.content %></p>
  15. </div>
  16. </div>
  17. <% }) %>
  18. <% } %>
  19. </div>
  20. </div>
  21. <script>
  22. </script>
  23. <%- include('footer.ejs') %>

new.ejs

  1. // new.ejs
  2. <%- include('header.ejs') %>
  3. <div class="container">
  4. <div class="row mt-5">
  5. <div class="col">
  6. <h3 class="text-center">请写下您的留言</h3>
  7. </div>
  8. </div>
  9. <div class="row">
  10. <div class="col">
  11. <form method="post" role="form">
  12. <div class="form-group">
  13. <label for="title">留言标题</label>
  14. <input type="text" class="form-control " id="title" name="title" placeholder="请输入标题...">
  15. </div>
  16. <div class="form-group">
  17. <label for="title">留言内容</label>
  18. <textarea rows="5" class="form-control " id="content" name="content" placeholder="请输入留言内容..."></textarea>
  19. </div>
  20. <div class="form-group">
  21. <input type="submit" class="btn btn-info float-right" value="提交留言">
  22. </div>
  23. </form>
  24. </div>
  25. </div>
  26. </div>
  27. <%- include('footer.ejs') %>

404.js

  1. <%- include('header.ejs') %>
  2. <div class="position-relative" style="height: 1000px;">
  3. <div class="h-75 ">
  4. <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>
  5. </div>
  6. </div>
  7. <%- include('footer.ejs') %>

header.js

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css">
  7. <title>GoBig</title>
  8. </head>
  9. <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"
}