API接口案例

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. 创建基本的服务器

《API接口案例》

 5. 创建 db 数据库操作模块

《API接口案例》

 6. 创建 user_ctrl 模块

《API接口案例》

7. 创建 user_router 模块

《API接口案例》

 8. 导入并挂载路由模块

《API接口案例》

9. 使用 try…catch 捕获异常 

《API接口案例》

 

    原文作者:Zhangzilnn
    原文地址: https://blog.csdn.net/weixin_45998300/article/details/123502916
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞