经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Node.js » 查看文章
node.js+express留言板功能实现示例
来源:jb51  时间:2021/9/22 8:36:40  对本文有异议

留言板

基于nodejs+express+art-template的留言板功能。包含列表界面、添加界面和发送留言功能。

所需类库

直接copy以下package.json 然后直接 npm install 或者yarn install 即可。

package.json所需内容如下。

  1. {
  2. "name": "nodejs_message_board",
  3. "version": "2021.09",
  4. "private": true,
  5. "scripts": {
  6. "start": "node app"
  7. },
  8. "dependencies": {
  9. "art-template": "^4.13.2",
  10. "debug": "~2.6.9",
  11. "express": "~4.16.1",
  12. "express-art-template": "^1.0.1"
  13. }
  14. }
  15.  

开源项目

本项目收录在【Node.js Study】nodejs开源学习项目 中的express_message_board 。欢迎大家学习和下载。

运行效果 localhost ,留言首页

在这里插入图片描述localhost/post ,

添加留言页面

在这里插入图片描述localhost/say?

name=xxx&message=xxx ,发送留言并重定向到首页功能

在这里插入图片描述 

项目结构

index.html

这是留言列表,也是首页。根据传过来的值渲染列表。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>留言板</title>
  6. <link rel="stylesheet" href="/public/css/bootstrap4.css" rel="external nofollow" rel="external nofollow" >
  7. </head>
  8. <body>
  9. <div class="header container">
  10. <div class="page-header">
  11. <h1>留言板 <small>留言列表</small></h1>
  12. <a class="btn btn-success" href="/post" rel="external nofollow" >发表留言</a>
  13. </div>
  14. </div>
  15. <div class="comments container">
  16. <ul class="list-group">
  17. {{each comments}}
  18. <li class="list-group-item">
  19. {{$value.name}}说: {{$value.message}}
  20. <span class="pull-right">{{$value.datetime}}</span>
  21. </li>
  22. {{/each}}
  23. </ul>
  24. </div>
  25. </body>
  26. </html>

post.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>留言板</title>
  6. <link rel="stylesheet" href="/public/css/bootstrap4.css" rel="external nofollow" rel="external nofollow" >
  7. </head>
  8. <body>
  9. <div class="header container">
  10. <div class="page-header">
  11. <h1><a href="/" rel="external nofollow" >留言板 <small>添加留言</small></a></h1>
  12. </div>
  13. </div>
  14. <div class="comments container">
  15. <form action="/say" method="GET">
  16. <div class="form-group">
  17. <label for="name">你的大名</label>
  18. <input type="text" id="name" name="name" class="form-control" placeholder="请输入姓名" required minlength="2" maxlength="10">
  19. </div>
  20. <div class="form-group">
  21. <label for="message">留言内容</label>
  22. <textarea id="message" name="message" class="form-control" placeholder="请输入留言内容" cols='30' rows='10' required minlength="5" maxlength="20"></textarea>
  23. </div>
  24. <button type="submit" class="btn btn-default">发表</button>
  25. </form>
  26. </div>
  27. </body>
  28. </html>
  29.  

route/index.js

这里是路由器

  1. const express = require('express');
  2. const router = express.Router();
  3.  
  4.  
  5. // 模拟首页留言列表数据
  6. var comments= {"comments":[
  7. {name:"AAA",message:"你用什么编辑器?WebStorem or VSCODE",datetime:"2021-1-1"},
  8. {name:"BBB",message:"今天天气真好?钓鱼or代码",datetime:"2021-1-1"},
  9. {name:"Moshow",message:"zhengkai.blog.csdn.net",datetime:"2021-1-1"},
  10. {name:"DDD",message:"哈与哈哈与哈哈哈的区别",datetime:"2021-1-1"},
  11. {name:"EEE",message:"王守义十三香还是iphone十三香",datetime:"2021-1-1"}
  12. ]}
  13.  
  14. /* by zhengkai.blog.csdn.net - 静态路由托管 */
  15. router.get('/', function(req, res, next) {
  16. res.render('index', comments);
  17. });
  18. router.get('/post', function(req, res, next) {
  19. res.render('post', comments);
  20. });
  21. router.get('/say', function(req, res, next) {
  22. console.log(req.query)
  23. console.log(req.url)
  24. const comment=req.query;
  25. comment.datetime='2021-10-01';
  26. comments.comments.unshift(comment);
  27. //重定向到首页,没有url后缀 localhost
  28. res.redirect('/');
  29. //直接渲染首页,有url后缀 localhost/say?xxxx=xxx
  30. //res.render('index', comments);
  31. });
  32.  
  33. module.exports = router;
  34.  

app.js

这里作为总控制

  1. //加载模块
  2. const http=require('http');
  3. const fs=require('fs');
  4. const url=require('url');
  5. const template=require('art-template');
  6. const path = require('path');
  7. const express = require('express');
  8. const router = express.Router();
  9. const app = express();
  10.  
  11.  
  12. // view engine setup
  13. app.set('views', path.join(__dirname, 'views'));
  14. app.set('view engine', 'html');
  15. app.engine('html',require('express-art-template'));
  16.  
  17. app.use('/public',express.static(path.join(__dirname, 'public')));
  18.  
  19. const indexRouter = require('./routes/index');
  20. app.use('/', indexRouter);
  21.  
  22.  
  23. //创建监听对象
  24. app.listen(80,function(){
  25. console.log('zhengkai.blog.csdn.net 项目启动成功 http://localhost')
  26. })

到此这篇关于node.js+express留言板功能实现示例的文章就介绍到这了,更多相关node.js express留言板内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号