๐ Introduction
In previous tutorials, you learned how to work with Express routes and middlewares, and how to connect Node.js to a MySQL database.
In this tutorial, we’ll combine everything and build a complete RESTful CRUD API — Create, Read, Update, and Delete — using Express.js, MySQL, and Postman for testing.
By the end of this guide, you’ll have a fully working API that can be used with any frontend or mobile application.
✅ Prerequisites
Before starting, make sure you have:
-
Node.js v18 or higher
-
MySQL is installed and running
-
Postman for API testing
-
Basic knowledge of JavaScript and Express.js
๐️ Step 1 — Create the Database
Open your MySQL terminal or phpMyAdmin and run:
This database will store user information for our API.
๐ Step 2 — Set Up the Project
Create a new project folder and initialize Node.js:
Install required dependencies:
Create the main application file:
๐ Step 3 — Connect to MySQL
Open app.js and add the following code:
This establishes a connection between your Node.js app and the MySQL database.
๐ Step 4 — Create CRUD API Routes
➕ Create User (POST)
๐ Read All Users (GET)
๐ Read User by ID (GET)
✏️ Update User (PUT)
๐️ Delete User (DELETE)
▶️ Step 5 — Start the Server
At the bottom of app.js, add:
Run the application:
Expected output:
๐งช Step 6 — Test with Postman
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/users | Create a new user |
| GET | /api/users | Get all users |
| GET | /api/users/:id | Get user by ID |
| PUT | /api/users/:id | Update user |
| DELETE | /api/users/:id | Delete user |
Example JSON Body (POST / PUT)
๐ Step 7 — Recommended Folder Structure
Current structure:
For larger projects, refactor to:
This keeps your code clean and scalable.
๐ง Summary
In this tutorial, you built a fully functional RESTful CRUD API using:
-
Express.js for routing
-
MySQL as the database
-
JSON-based API responses
-
Postman for testing
This API structure is production-ready and can easily connect to React, Vue, mobile apps, or any frontend framework.
.jpg)
