Home  >  Article  >  Web Front-end  >  nodejs implements post-it message board

nodejs implements post-it message board

王林
王林Original
2023-05-08 11:53:07484browse

Post-it notes are an effective way for people to record daily affairs, memos and notifications, and modern technology has migrated them to the digital realm. This article will introduce how to use Node.js to create a simple sticky note message board that allows users to create, edit and delete sticky notes.

First, you need to install Node.js and Express framework. Create the project using the following command:

mkdir notepad
cd notepad
npm init
npm install express --save

Next, create a file called index.js and add the following content:

const express = require('express');
const app = express();
const PORT = 3000;

// 配置视图模板引擎
app.set('view engine', 'ejs');

// 配置静态资源
app.use(express.static('public'));

// 路由
app.get('/', (req, res) => {
  res.render('index');
});

// 启动服务器
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

In this snippet, We first imported the Express framework and created an application named app. Next, we set the application's view template engine to ejs and use the express.static middleware to publish the static resources in the public directory, such as styles Tables, JavaScript files, images, etc. Then, we define a route value of / and call the res.render method in the returned response to render the index.ejs view template. Finally, we start the server on port 3000 and print a message to the console to indicate that the server is running.

Next, create a template named index.ejs and add the following content:




  
  Node.js Notepad
  

Node.js Notepad

<% for(let note of notes) { %>
x

<%= note.content %>

<% } %>

This template defines a page with two parts, one is a form for entering new sticky notes, and the other is a list of existing sticky notes. The scripts.js file is introduced in the