node.js+express留言板功能實(shí)現(xiàn)示例
留言板
基于nodejs+express+art-template的留言板功能。包含列表界面、添加界面和發(fā)送留言功能。
所需類庫(kù)
直接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 ,留言首頁(yè)
localhost/post ,
添加留言頁(yè)面
localhost/say?
name=xxx&message=xxx ,發(fā)送留言并重定向到首頁(yè)功能
項(xiàng)目結(jié)構(gòu)
index.html
這是留言列表,也是首頁(yè)。根據(jù)傳過(guò)來(lái)的值渲染列表。
<!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}}說(shuō): {{$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();
// 模擬首頁(yè)留言列表數(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);
//重定向到首頁(yè),沒(méi)有url后綴 localhost
res.redirect('/');
//直接渲染首頁(yè),有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)簡(jiǎn)單攔截器功能示例
相關(guān)文章
VsCode無(wú)法識(shí)別node問(wèn)題解決過(guò)程
這篇文章主要給大家介紹了關(guān)于VsCode無(wú)法識(shí)別node問(wèn)題解決的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07
nodejs連接mysql數(shù)據(jù)庫(kù)及基本知識(shí)點(diǎn)詳解
這篇文章主要介紹了nodejs連接mysql數(shù)據(jù)庫(kù),結(jié)合實(shí)例形式總結(jié)分析了nodejs連接與操作mysql數(shù)據(jù)庫(kù)的相關(guān)模板、配置及mysql數(shù)據(jù)庫(kù)查詢、添加數(shù)據(jù)等操作技巧,需要的朋友可以參考下2018-03-03
關(guān)于express與koa的使用對(duì)比詳解
很多人都在問(wèn)到底該用Koa還是express,所以下面這篇文章就來(lái)給大家再次的對(duì)比了關(guān)于express與koa的相關(guān)資料,通過(guò)對(duì)比大家可以更好的進(jìn)行選擇,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
基于Node.js實(shí)現(xiàn)nodemailer郵件發(fā)送
Nodemailer 是一個(gè)簡(jiǎn)單易用的 Node.JS 郵件發(fā)送模塊(通過(guò) SMTP,sendmail,或者 Amazon SES),支持 unicode,你可以使用任何你喜歡的字符集。Nodemailer是一個(gè)簡(jiǎn)單易用的Node.js郵件發(fā)送組件,需要的朋友可以參考下2016-01-01
npm安裝報(bào)錯(cuò)npm ERR! Error: EPERM: operation&
這篇文章主要為大家介紹了npm安裝報(bào)錯(cuò)npm ERR! Error: EPERM: operation not permitted解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

