Node.js 使用 MySQL 进行 CRUD 操作的示例

2025-05-28

Node.js 使用 MySQL 进行 CRUD 操作的示例

本教程将指导您完成使用 Expressjs for Rest API 构建带有 MySQL 数据库的简单 Node.js CRUD 操作的步骤。

全文:使用 Express 和 MySQL 构建 Node.js Rest API

应用程序概述

我们将使用 MySQL - Rest Apis 构建 Node.js CRUD 操作,用于创建、检索、更新和删除客户。

首先,我们从一个 Express Web 服务器开始。接下来,我们添加 MySQL 数据库的配置,创建Customer模型,编写控制器。然后,我们定义处理所有 CRUD 操作的路由:

方法 网址 行动
得到 /顾客 获取所有客户
得到 /客户/42 获取 id=42 的客户
邮政 /顾客 添加新客户
/客户/42 更新 ID=42 的客户
删除 /客户/42 删除 id=42 的客户
删除 /顾客 删除所有客户

最后,我们将使用 Postman 测试 Rest Apis。

我们的项目结构将是这样的:

nodejs-rest-api-express-mysql-项目结构

测试 API

使用命令运行我们的 Node.js 应用程序:node server.js
控制台显示:

Server is running on port 3000.
Successfully connected to the database.
Enter fullscreen mode Exit fullscreen mode

使用 Postman,我们将测试上述所有 API。

  • POST /customers使用Api创建新客户

  • nodejs-rest-api-express-mysql-测试-创建

    创建一些新的客户后,我们可以检查 MySQL 表:

    mysql> SELECT * FROM customers;
    +----+--------------------+--------+--------+
    | id | email              | name   | active |
    +----+--------------------+--------+--------+
    |  1 | bezkoder@gmail.com | zKoder |      1 |
    |  2 | jack123@gmail.com  | Jack   |      0 |
    |  3 | drhelen@gmail.com  | Helen  |      0 |
    +----+--------------------+--------+--------+
    
    Enter fullscreen mode Exit fullscreen mode

  • GET /customers使用Api检索所有客户

  • nodejs-rest-api-express-mysql-测试-检索全部

  • GET /customers/:customerId使用Api通过 ID 检索单个客户

  • nodejs-rest-api-express-mysql-测试-检索一个

  • PUT /customers/:customerId使用Api更新客户

  • nodejs-rest-api-express-mysql-测试更新

    customers更新某一行后检查表:

    mysql> SELECT * FROM customers;
    +----+--------------------+----------+--------+
    | id | email              | name     | active |
    +----+--------------------+----------+--------+
    |  1 | bezkoder@gmail.com | zKoder   |      1 |
    |  2 | jack123@gmail.com  | Jack     |      0 |
    |  3 | drhelen@gmail.com  | Dr.Helen |      1 |
    +----+--------------------+----------+--------+
    
    Enter fullscreen mode Exit fullscreen mode

  • DELETE /customers/:customerId使用Api删除客户

  • nodejs-rest-api-express-mysql-测试-删除一个

    id=2 的客户已从表中删除customers

    mysql> SELECT * FROM customers;
    +----+--------------------+----------+--------+
    | id | email              | name     | active |
    +----+--------------------+----------+--------+
    |  1 | bezkoder@gmail.com | zKoder   |      1 |
    |  3 | drhelen@gmail.com  | Dr.Helen |      1 |
    +----+--------------------+----------+--------+
    
    Enter fullscreen mode Exit fullscreen mode

  • DELETE /customers使用Api删除所有客户

  • nodejs-rest-api-express-mysql-测试-删除所有

    现在表中没有行customers

    mysql> SELECT * FROM customers;
    Empty set (0.00 sec)
    
    Enter fullscreen mode Exit fullscreen mode

    有关分步说明和 Github 源代码,请访问:
    使用 Express 和 MySQL 构建 Node.js Rest API

    进一步阅读

    相关文章:

    全栈:

    安全性:Node.js – JWT 身份验证和授权示例

    部署:

    Node.js 和 MySQL 关联:

    文章来源:https://dev.to/tienbku/node-js-crud-operation-with-mysql-example-1gme
    PREV
    使用 MySQL 数据库的 Node.js Express 登录示例
    NEXT
    FastAPI 和 Pydantic 的未来一片光明