Node+express+ejs+mysql制作查询数据呈现前端页面

一、实现样式

student

二、页面结构

1、文档结构
结构
2、数据库mysql结构
mysql
3、使用到的模块
package

三、核心思路

该案例使用node+express+ejs+mysql共同实现,实现效果很简单,查询数据库,获取数据后,呈现在前端页面上面,连接mysql或者数据很简单,核心点在使用ejs模块,语法为 <%= %>

1、使用mysql模块连接mysql数据库,获取数据results,获取到的数据为一个数组形式
mysql.js

const mysql = require('mysql')

const db = mysql.createPool({
    host: '192.168.19.133',
    user: 'root',
    password: 'root123',
    database: 'node_mysql'
})

// 共享db
module.exports = db

// 连接测试mysql
// db.query('select * from ev_user', (err, results) => {
//     if (err) {
//         return results.send({ status: 1, msg: err, message })
//     }
//     console.log(results);
// })

2、在使用esj之前先配置esj

app.set('view engine', 'ejs'); //设置ejs模块
app.set('views', __dirname + '/views')  //文件路径

核心esj代码

exports.userlist = (req, res) => {
    const sqlstr = 'select * from ev_students'
    db.query(sqlstr, (err, results) => {
        if (err) return res.cc(err)
        console.log(results);
        res.render('students', { results })
    })

}

3、在上面views路径下面新建一个students.esj文件,该类型文件可以使用html来编写
students.ejs

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./students.css">
    <title>用户列表</title>
</head>
<style>
    h1 {
        text-align: center;
    }
    
    table {
        border-collapse: collapse;
        margin: 0 auto;
        text-align: center;
        width: 800px;
    }
    
    table td,
    table th {
        border: 1px solid #cad9ea;
        color: #666;
        height: 30px;
    }
    
    table thead th {
        background-color: #CCE8EB;
        width: 100px;
    }
    
    table tr:nth-child(odd) {
        background: #fff;
    }
    
    table tr:nth-child(even) {
        background: #F5FAFA;
    }
</style>

</style>

<body>
    <h1>学生信息表</h1>
    <table>
        <tr>
            <th>编号</th>
            <th>学工号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>手机号</th>
            <th>班级</th>
            <th>年级</th>
            <th>身份证号</th>
        </tr>
        <% for(let i=0;i<results.length;i++){ %>
            <tr>
                <td>
                    <%= results[i].id  %>
                </td>
                <td>
                    <%= results[i].idserial  %>
                </td>
                <td>
                    <%= results[i].username  %>
                </td>
                <td>
                    <%= results[i].sex  %>
                </td>
                <td>
                    <%= results[i].phone  %>
                </td>
                <td>
                    <%= results[i].class  %>
                </td>
                <td>
                    <%= results[i].major  %>
                </td>
                <td>
                    <%= results[i].cardid  %>
                </td>
            </tr>
            <%   }   %>

    </table>

</body>

</html>

使用nodemon执行入口文件app.js后,打开对应路由,就能将数据呈现在页面上面

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的示例代码,实现了一个基本的博客系统。 首先,我们需要创建一个新的node.js项目,并在项目中安装expressmysql模块。 ```bash mkdir blog cd blog npm init -y npm install express mysql --save ``` 然后,我们需要创建一个app.js文件,并在其中配置express应用程序。 ```javascript const express = require('express'); const mysql = require('mysql'); const app = express(); // 配置中间件 app.use(express.urlencoded({ extended: false })); app.use(express.json()); app.use(express.static('public')); // 连接数据库 const db = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'blog' }); db.connect((err) => { if (err) { console.error('error connecting: ' + err.stack); return; } console.log('connected as id ' + db.threadId); }); // 设置路由 app.get('/', (req, res) => { db.query('SELECT * FROM posts', (err, results) => { if (err) throw err; res.render('index.ejs', { posts: results }); }); }); app.get('/post/:id', (req, res) => { const id = req.params.id; db.query('SELECT * FROM posts WHERE id = ?', [id], (err, results) => { if (err) throw err; res.render('post.ejs', { post: results[0] }); }); }); app.get('/create', (req, res) => { res.render('create.ejs'); }); app.post('/create', (req, res) => { const title = req.body.title; const content = req.body.content; db.query('INSERT INTO posts SET ?', { title, content }, (err, results) => { if (err) throw err; res.redirect('/'); }); }); // 启动服务器 app.listen(3000, () => { console.log('Server started on port 3000'); }); ``` 在上面的代码中,我们使用了ejs模板引擎来渲染HTML页面。我们需要在项目中创建一个views文件夹,并在其中创建index.ejs、post.ejs和create.ejs文件。 index.ejs: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Blog</title> </head> <body> <h1>Blog</h1> <ul> <% for (let post of posts) { %> <li> <a href="/post/<%= post.id %>"><%= post.title %></a> </li> <% } %> </ul> <a href="/create">Create a new post</a> </body> </html> ``` post.ejs: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><%= post.title %></title> </head> <body> <h1><%= post.title %></h1> <p><%= post.content %></p> <a href="/">Back to index</a> </body> </html> ``` create.ejs: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Create a new post</title> </head> <body> <h1>Create a new post</h1> <form action="/create" method="POST"> <input type="text" name="title" placeholder="Title"> <textarea name="content" placeholder="Content"></textarea> <button type="submit">Create</button> </form> </body> </html> ``` 最后,我们需要在项目中创建一个public文件夹,并在其中创建一个style.css文件,来为页面添加样式。 ```css body { font-family: Arial, sans-serif; } h1 { font-size: 32px; font-weight: bold; } ul { list-style: none; padding: 0; } li { margin-bottom: 10px; } a { text-decoration: none; color: blue; } input[type="text"], textarea { display: block; width: 100%; margin-bottom: 10px; } button[type="submit"] { display: block; margin-top: 10px; } ``` 以上就是一个简单的node.js+express+mysql实现博客系统的示例代码,可以根据实际情况进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柒月VII

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值