当前位置:网站首页>vue3 访问数据库中的数据
vue3 访问数据库中的数据
2022-08-02 03:23:00 【HardworkLiao】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、需要的工具
- Node.js
- VsCode
- Express
- Mysql2
- MYSQL Workbench
二、使用步骤
(1)项目结构

(2)创建项目
在VsCode新建名为code的文件夹
(3)初始化项目
npm init -y
- package.json
//新增type节点值为module
{
"type":"module",
"name": "database-assess",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"mysql2": "^2.3.3"
}
}
(4)编写模块
- 路由模块
- 引入express
npm install express
- use_router.js
import express from 'express'
import {
getAllUser } from '../controller/user_ctrl.js'
// 定义express的router方法
const router = new express.Router()
router.get('/user', getAllUser)
export default router
- 数据库模块
- 引入mysql
npm install mysql2
- index.js
import mysql from 'mysql2'
const pool = mysql.createPool({
host: '127.0.0.1',
port: '3306',
database: 'mysql_db01',
user: 'root',
password: ''//数据库密码
})
export default pool.promise()
- 数据控制模块
- user_ctrl.js
import db from '../db/index.js'
export async function getAllUser(req, res) {
try {
const [row] = await db.query('select id,username,nickname from ev_users')
res.send({
status: 0,
message: '获取用户数据成功!',
data: row
})
} catch (err) {
res.send({
status: 1,
message: '获取用户列表错误',
desc: err.message
})
}
}
- app.js
// ES6语法引入express模块
import express from 'express'
import userRouter from './router/user_router.js'
const app = express()
// 定义路由
app.use('/api', userRouter)
// 监听8080端口
app.listen(80, () => {
console.log('serve running in http://127.0.0.1')
})
5.数据库表
(5)测试
- 项目目录下运行
node app.js - 访问127.0.0.1/api/user
- 数据获取成功

(6)项目地址
边栏推荐
- subprocess.CalledProcessError: Command 'pip install 'thop'' returned non-zero exit status 1.
- __dirname
- 云服务器安装部署Nacos2.0.4版本
- parser = argparse.ArgumentParser() parsing
- ssm各类配置模板
- np.unique() function
- 【博学谷学习记录】超强总结,用心分享 | 软件测试 接口测试基础
- C语言 内联函数
- js takes the value of a feature at a certain position in the string, such as Huawei=> Huawei
- 每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
猜你喜欢
随机推荐
parser = argparse.ArgumentParser() parsing
新工程加载YOLOV6的预训练权重问题
Debian 10 NTP Service Configuration
AttributeError: Can't get attribute 'SPPF' on
ssm各类配置模板
Phospholipid-polyethylene glycol-thiol, DSPE-PEG-Thiol, DSPE-PEG-SH, MW: 5000
SSM integration
pyppeteer使用样例脚本
Amazon sellers how to improve the conversion
知识工程作业2:知识工程相关领域介绍
Circular linked list---------Joseph problem
nucleo stm32 h743 FREERTOS CUBE MX配置小记录
解决MySQL创建子视图并查看的时候,字符集报错问题
第一篇博客
Redis simple study notes
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
钟表刻度线
DOM操作---放大镜案例
Dynamic proxy tool class









