Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use node.js and express to realize message board function

Shulou Source: shulou.com Published: 2022-06-03 10:08:41 10月03日 Update

This article will explain in detail how to use node.js and express to achieve message board function. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Message Board

Message board function based on nodejs+express+art-template. Includes list interface, add interface and send message function.

Required class library

Directly copy the following package.json and then directly npm install or yarn install.

The requirements for package.json are as follows.

{"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"}} Open Source Project

This project includes express_message_board in the [Node.js Study] nodejs open source learning project. Welcome to learn and download.

Run effect localhost, leave a message on the home page

Localhost/post

Add message page

Localhost/say?

Name=xxx&message=xxx, send messages and redirect to the home page function

Project structure

Index.html

This is the list of messages and the home page. Render the list according to the values passed.

Message board message list message list {{each comments}} {{$value.name}} say: {{$value.message}} {{/ each}}

Post.html

Add a message to the message board. Your famous message content is published.

Route/index.js

This is the router.

Const express = require ('express'); const router = express.Router (); / / simulate the home page message list data var comments= {"comments": [{name: "AAA", message: "what editor do you use? WebStorem or VSCODE ", datetime:" 2021-1-1 "}, {name:" BBB ", message:" what a beautiful day? Fishing or Code ", datetime:" 2021-1-1 "}, {name:" Moshow ", message:" zhengkai.blog.csdn.net ", datetime:" 2021-1-1 "}, {name:" DDD ", message:" the difference between and ", datetime:" 2021-1-1 "}, {name:" EEE ", message:" Wang Shouyi thirteen incense or iPhone thirteen incense " Datetime: "2021-1-1"}]} / * by zhengkai.blog.csdn.net-static route hosting * / 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) / / Redirect to the home page without the url suffix localhost res.redirect ('/'); / / render the home page directly with the url suffix localhost/say?xxxx=xxx / / res.render ('index', comments);}); module.exports = router

App.js

Here as total control.

/ / load module 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 setupapp.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) / / create a listening object app.listen (80dint function () {console.log ('zhengkai.blog.csdn.net project started successfully). This is the end of the article http://localhost')}) on "how to use node.js and express to achieve message board function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Message board home page function project content article suffix more interface routing learning good practical success code name weather object effect data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia vpn OPPO Reno Docker