如何使用node.js和express实现留言板功能

发布时间:2021-09-25 08:27:48 作者:小新
来源:亿速云 阅读:214

这篇文章将为大家详细讲解有关如何使用node.js和express实现留言板功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

留言板

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

所需类库

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

package.json所需内容如下。

{
  "name": "nodejs_message_board",
  "version": "2021.09",
  "private": true,
  "scripts": {
    "start": "node app"
  },
  "dependencies": {
    "art-template": "^4.13.2",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "express-art-template": "^1.0.1"
  }
}

开源项目

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

运行效果 localhost ,留言首页

如何使用node.js和express实现留言板功能localhost/post ,

添加留言页面

如何使用node.js和express实现留言板功能localhost/say?

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

如何使用node.js和express实现留言板功能 

项目结构

index.html

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

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>留言板</title>
  <link rel="stylesheet" href="/public/css/bootstrap4.css" rel="external nofollow"  rel="external nofollow" >
</head>
<body>
<div class="header container">
  <div class="page-header">
    <h2>留言板 <small>留言列表</small></h2>
    <a class="btn btn-success" href="/post" rel="external nofollow" >发表留言</a>
  </div>
</div>
<div class="comments container">
  <ul class="list-group">
    {{each comments}}
    <li class="list-group-item">
      {{$value.name}}说:  {{$value.message}}
      <span class="pull-right">{{$value.datetime}}</span>
    </li>
    {{/each}}
  </ul>
</div>
</body>
</html>

post.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <link rel="stylesheet" href="/public/css/bootstrap4.css" rel="external nofollow"  rel="external nofollow" >
</head>
<body>
<div class="header container">
    <div class="page-header">
        <h2><a href="/" rel="external nofollow"  >留言板 <small>添加留言</small></a></h2>
    </div>
</div>
<div class="comments container">
    <form action="/say" method="GET">
        <div class="form-group">
            <label for="name">你的大名</label>
            <input type="text" id="name" name="name" class="form-control" placeholder="请输入姓名" required minlength="2" maxlength="10">
        </div>
        <div class="form-group">
            <label for="message">留言内容</label>
            <textarea id="message" name="message" class="form-control" placeholder="请输入留言内容" cols='30' rows='10' required minlength="5" maxlength="20"></textarea>
        </div>
        <button type="submit" class="btn btn-default">发表</button>
    </form>
</div>
</body>
</html>

route/index.js

这里是路由器

const express = require('express');
const router = express.Router();


// 模拟首页留言列表数据
var comments= {"comments":[
    {name:"AAA",message:"你用什么编辑器?WebStorem or VSCODE",datetime:"2021-1-1"},
    {name:"BBB",message:"今天天气真好?钓鱼or代码",datetime:"2021-1-1"},
    {name:"Moshow",message:"zhengkai.blog.csdn.net",datetime:"2021-1-1"},
    {name:"DDD",message:"哈与哈哈与哈哈哈的区别",datetime:"2021-1-1"},
    {name:"EEE",message:"王守义十三香还是iphone十三香",datetime:"2021-1-1"}
]}

/* by zhengkai.blog.csdn.net - 静态路由托管 */
router.get('/', function(req, res, next) {
    res.render('index', comments);
});
router.get('/post', function(req, res, next) {
    res.render('post', comments);
});
router.get('/say', function(req, res, next) {
    console.log(req.query)
    console.log(req.url)
    const comment=req.query;
    comment.datetime='2021-10-01';
    comments.comments.unshift(comment);
    //重定向到首页,没有url后缀 localhost
    res.redirect('/');
    //直接渲染首页,有url后缀 localhost/say?xxxx=xxx
    //res.render('index', comments);
});

module.exports = router;

app.js

这里作为总控制

//加载模块
const http=require('http');
const fs=require('fs');
const url=require('url');
const template=require('art-template');
const path = require('path');
const express = require('express');
const router = express.Router();
const app = express();


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('html',require('express-art-template'));

app.use('/public',express.static(path.join(__dirname, 'public')));

const indexRouter = require('./routes/index');
app.use('/', indexRouter);


//创建监听对象
app.listen(80,function(){
  console.log('zhengkai.blog.csdn.net 项目启动成功 http://localhost')
})

关于“如何使用node.js和express实现留言板功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. Kubernetes怎么实现留言板功能
  2. Node.js express generator

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

node.js express

上一篇:MybatisPlus开启或关闭二级缓存方法是怎样的

下一篇:python中数据类型的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》