当前位置:网站首页>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)项目地址
边栏推荐
- STM32 CAN过滤器
- 错误:with open(txt_path,‘r‘) as f: FileNotFoundError: [Errno 2] No such file or directory:
- 【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
- np.unique()函数
- The usage of json type field in mysql
- [Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
- ModuleNotFoundError No module named 'xxx' possible solutions
- 微信小程序云开发如何将页面生成为pdf?
- canvas--pie chart
- 解决glob()返回文件排序不一致问题&onnx本地按照安装方法
猜你喜欢

URL module

远程调试、无cuDnn、自定义模块无法导入问题记录

科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺

小程序 van-cell 换行能左对齐问题

URL URL

利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置

Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000

ModuleNotFoundError No module named 'xxx' possible solutions

URL模块

Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
随机推荐
STM32 map文件解析
STM32 触发HardFault_Handler如何查找原因
mysql阶段总结
Advanced gradient of skeleton effect, suitable for waiting for pictures
npm and package.json
URL模块
C语言 void和void *(无类型指针)
SOCKS5
基本运算符
环形链表---------约瑟夫问题
FreeRTOS内核详解(1) —— 临界段保护原理
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu
小程序 van-cell 换行能左对齐问题
ssm各类配置模板
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
我的小笔记 =》其他东东
URL module
Dynamic proxy tool class