Node.js CRUD Operation with MySQL example

This tutorial will guide you through the steps of building a simple Node.js CRUD Operation with MySQL database using Expressjs for Rest API.

Full Article: Build Node.js Rest APIs with Express & MySQL

Application overview

We will build Node.js CR…


This content originally appeared on DEV Community and was authored by Tien Nguyen

This tutorial will guide you through the steps of building a simple Node.js CRUD Operation with MySQL database using Expressjs for Rest API.

Full Article: Build Node.js Rest APIs with Express & MySQL

Application overview

We will build Node.js CRUD Operation with MySQL - Rest Apis for creating, retrieving, updating & deleting Customers.

First, we start with an Express web server. Next, we add configuration for MySQL database, create Customer model, write the controller. Then we define routes for handling all CRUD operations:

Methods Urls Actions
GET /customers get all Customers
GET /customers/42 get Customer with id=42
POST /customers add new Customer
PUT /customers/42 update Customer with id=42
DELETE /customers/42 remove Customer with id=42
DELETE /customers remove all Customers

Finally, we're gonna test the Rest Apis using Postman.

Our project structure will be like:

nodejs-rest-api-express-mysql-project-structure

Test the APIs

Run our Node.js application with command: node server.js.
The console shows:

Server is running on port 3000.
Successfully connected to the database.

Using Postman, we're gonna test all the Apis above.

  • Create a new Customer using POST /customers Api

  • nodejs-rest-api-express-mysql-test-create

    After creating some new Customers, we can check MySQL table:

    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 |
    +----+--------------------+--------+--------+
    

  • Retrieve all Customers using GET /customers Api

  • nodejs-rest-api-express-mysql-test-retrieve-all

  • Retrieve a single Customer by id using GET /customers/:customerId Api

  • nodejs-rest-api-express-mysql-test-retrieve-one

  • Update a Customer using PUT /customers/:customerId Api

  • nodejs-rest-api-express-mysql-test-update

    Check customers table after a row was updated:

    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 |
    +----+--------------------+----------+--------+
    

  • Delete a Customer using DELETE /customers/:customerId Api

  • nodejs-rest-api-express-mysql-test-delete-one

    Customer with id=2 was removed from customers table:

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

  • Delete all Customers using DELETE /customers Api

  • nodejs-rest-api-express-mysql-test-delete-all

    Now there are no rows in customers table:

    mysql> SELECT * FROM customers;
    Empty set (0.00 sec)
    

    For step by step instruction and Github source code, please visit:
    Build Node.js Rest APIs with Express & MySQL

    Further Reading

    Related Posts:

    Fullstack:

    Security: Node.js – JWT Authentication & Authorization example
    Deployment: Deploying/Hosting Node.js app on Heroku with MySQL database

    Node.js & MySQL Associations:


    This content originally appeared on DEV Community and was authored by Tien Nguyen


    Print Share Comment Cite Upload Translate Updates
    APA

    Tien Nguyen | Sciencx (2021-08-11T11:37:52+00:00) Node.js CRUD Operation with MySQL example. Retrieved from https://www.scien.cx/2021/08/11/node-js-crud-operation-with-mysql-example/

    MLA
    " » Node.js CRUD Operation with MySQL example." Tien Nguyen | Sciencx - Wednesday August 11, 2021, https://www.scien.cx/2021/08/11/node-js-crud-operation-with-mysql-example/
    HARVARD
    Tien Nguyen | Sciencx Wednesday August 11, 2021 » Node.js CRUD Operation with MySQL example., viewed ,<https://www.scien.cx/2021/08/11/node-js-crud-operation-with-mysql-example/>
    VANCOUVER
    Tien Nguyen | Sciencx - » Node.js CRUD Operation with MySQL example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/11/node-js-crud-operation-with-mysql-example/
    CHICAGO
    " » Node.js CRUD Operation with MySQL example." Tien Nguyen | Sciencx - Accessed . https://www.scien.cx/2021/08/11/node-js-crud-operation-with-mysql-example/
    IEEE
    " » Node.js CRUD Operation with MySQL example." Tien Nguyen | Sciencx [Online]. Available: https://www.scien.cx/2021/08/11/node-js-crud-operation-with-mysql-example/. [Accessed: ]
    rf:citation
    » Node.js CRUD Operation with MySQL example | Tien Nguyen | Sciencx | https://www.scien.cx/2021/08/11/node-js-crud-operation-with-mysql-example/ |

    Please log in to upload a file.




    There are no updates yet.
    Click the Upload button above to add an update.

    You must be logged in to translate posts. Please log in or register.