1. 案例需求
基于 MySQL 数据库 + Express 对外提供用户列表的 API 接口服务。用到的技术点如下: 第三方包 express 和 mysql2
ES6 模块化
Promise
async/await
2. 主要的实现步骤
搭建项目的基本结构
创建基本的服务器
创建 db 数据库操作模块
创建 user_ctrl 业务模块
创建 user_router 路由模块
3. 搭建项目的基本结构
启用 ES6 模块化支持:在 package.json 中声明 “type”: “module”
安装第三方依赖包:运行 npm install express@4.17.1 mysql2@2.2.5
4. 创建基本的服务器
5. 创建 db 数据库操作模块
6. 创建 user_ctrl 模块
7. 创建 user_router 模块
8. 导入并挂载路由模块
9. 使用 try…catch 捕获异常