node.js+express留言板功能實(shí)現(xiàn)示例
留言板
基于nodejs+express+art-template的留言板功能。包含列表界面、添加界面和發(fā)送留言功能。
所需類庫
直接copy以下package.json 然后直接 npm install 或者yarn install 即可。
package.json所需內(nèi)容如下。
{
"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"
}
}
開源項(xiàng)目
本項(xiàng)目收錄在【Node.js Study】nodejs開源學(xué)習(xí)項(xiàng)目 中的express_message_board 。歡迎大家學(xué)習(xí)和下載。
運(yùn)行效果 localhost ,留言首頁
localhost/post ,
添加留言頁面
localhost/say?
name=xxx&message=xxx ,發(fā)送留言并重定向到首頁功能
項(xiàng)目結(jié)構(gòu)
index.html
這是留言列表,也是首頁。根據(jù)傳過來的值渲染列表。
<!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">
<h1>留言板 <small>留言列表</small></h1>
<a class="btn btn-success" href="/post" rel="external nofollow" >發(fā)表留言</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">
<h1><a href="/" rel="external nofollow" >留言板 <small>添加留言</small></a></h1>
</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="請(qǐng)輸入姓名" required minlength="2" maxlength="10">
</div>
<div class="form-group">
<label for="message">留言內(nèi)容</label>
<textarea id="message" name="message" class="form-control" placeholder="請(qǐng)輸入留言內(nèi)容" cols='30' rows='10' required minlength="5" maxlength="20"></textarea>
</div>
<button type="submit" class="btn btn-default">發(fā)表</button>
</form>
</div>
</body>
</html>
route/index.js
這里是路由器
const express = require('express');
const router = express.Router();
// 模擬首頁留言列表數(shù)據(jù)
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:"哈與哈哈與哈哈哈的區(qū)別",datetime:"2021-1-1"},
{name:"EEE",message:"王守義十三香還是iphone十三香",datetime:"2021-1-1"}
]}
/* by zhengkai.blog.csdn.net - 靜態(tài)路由托管 */
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);
//創(chuàng)建監(jiān)聽對(duì)象
app.listen(80,function(){
console.log('zhengkai.blog.csdn.net 項(xiàng)目啟動(dòng)成功 http://localhost')
})
到此這篇關(guān)于node.js+express留言板功能實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)node.js express留言板內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- node.js中koa和express的差異對(duì)比
- Node.js中Express框架使用axios同步請(qǐng)求(async+await)實(shí)現(xiàn)方法
- node.js使用express-jwt報(bào)錯(cuò):expressJWT?is?not?a?function解決
- Node.js使用express寫接口的具體代碼
- Node.js?express中的身份認(rèn)證的實(shí)現(xiàn)
- 使用Express+Node.js對(duì)mysql進(jìn)行增改查操作?
- node.js三個(gè)步驟實(shí)現(xiàn)一個(gè)服務(wù)器及Express包使用
- Node.js中Express框架的使用教程詳解
- node.js使用express-fileupload中間件實(shí)現(xiàn)文件上傳
- Node.js+express+socket實(shí)現(xiàn)在線實(shí)時(shí)多人聊天室
- Express框架實(shí)現(xiàn)簡單攔截器功能示例
相關(guān)文章
輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請(qǐng)求
這篇文章主要介紹了輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請(qǐng)求,本文告訴你如何實(shí)現(xiàn)在node.js中處理POST請(qǐng)求,需要的朋友可以參考下2014-12-12
使用node-media-server搭建一個(gè)簡易的流媒體服務(wù)器
這篇文章主要介紹了使用node-media-server搭建一個(gè)簡易的流媒體服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
nodejs mysql 實(shí)現(xiàn)分頁的方法
本篇文章主要介紹了nodejs mysql 實(shí)現(xiàn)分頁的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Node.js設(shè)置CORS跨域請(qǐng)求中多域名白名單的方法
這篇文章主要介紹了Node.js設(shè)置CORS跨域請(qǐng)求中多域名白名單的方法,文中通過示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
Node.js前后端交互實(shí)現(xiàn)用戶登陸的實(shí)踐
本文主要介紹了Node.js前后端交互實(shí)現(xiàn)用戶登陸的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
node.js 中間件express-session使用詳解
這篇文章主要給大家介紹了node.js中間件express-session使用的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05
詳解基于Node.js的HTTP/2 Server實(shí)踐
HTTP/2目前已經(jīng)逐漸的在各大網(wǎng)站上開始使用,這篇文章主要介紹了詳解基于Node.js的HTTP/2 Server實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05

