欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

node.js+express留言板功能實(shí)現(xiàn)示例

 更新時(shí)間:2021年09月21日 09:07:24   作者:Moshow鄭鍇  
本文介紹基于nodejs+express+art-template的留言板功能。包含列表界面、添加界面和發(fā)送留言功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

留言板

基于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="請輸入姓名" 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="請輸入留言內(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)聽對象
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請求

    輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請求

    這篇文章主要介紹了輕松創(chuàng)建nodejs服務(wù)器(10):處理POST請求,本文告訴你如何實(shí)現(xiàn)在node.js中處理POST請求,需要的朋友可以參考下
    2014-12-12
  • 使用node-media-server搭建一個(gè)簡易的流媒體服務(wù)器

    使用node-media-server搭建一個(gè)簡易的流媒體服務(wù)器

    這篇文章主要介紹了使用node-media-server搭建一個(gè)簡易的流媒體服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • nodejs mysql 實(shí)現(xiàn)分頁的方法

    nodejs mysql 實(shí)現(xiàn)分頁的方法

    本篇文章主要介紹了nodejs mysql 實(shí)現(xiàn)分頁的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Node.js設(shè)置CORS跨域請求中多域名白名單的方法

    Node.js設(shè)置CORS跨域請求中多域名白名單的方法

    這篇文章主要介紹了Node.js設(shè)置CORS跨域請求中多域名白名單的方法,文中通過示例代碼介紹的非常詳細(xì),相信對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • Node.js前后端交互實(shí)現(xiàn)用戶登陸的實(shí)踐

    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使用詳解

    這篇文章主要給大家介紹了node.js中間件express-session使用的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • 詳解redis在nodejs中的應(yīng)用

    詳解redis在nodejs中的應(yīng)用

    本篇文章給大家詳細(xì)分析了redis在nodejs中的應(yīng),對此知識(shí)點(diǎn)有興趣的朋友可以跟著學(xué)習(xí)下。
    2018-05-05
  • NodeJS中配置請求代理服務(wù)器方式

    NodeJS中配置請求代理服務(wù)器方式

    這篇文章主要介紹了NodeJS中配置請求代理服務(wù)器方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 詳解基于Node.js的HTTP/2 Server實(shí)踐

    詳解基于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
  • 淺談JS前端模塊化的幾種規(guī)范

    淺談JS前端模塊化的幾種規(guī)范

    這篇文章主要介紹了JS前端模塊化的幾種規(guī)范,對前端模塊化感興趣的同學(xué),可以參考下
    2021-05-05

最新評論