A simple CRUD (Create, Read, Update, Delete) REST API built using Node.js, Express, MySQL, and Docker. This project demonstrates a clean and modular folder structure for scalability and maintainability.
- CRUD Operations: Manage products through Create, Read, Update, and Delete operations.
- MySQL Database: Uses MySQL as the database, running in a Docker container.
- Modular Structure: Organized codebase for controllers, routes, and database connections.
- Environment Configuration:
.env
file for managing sensitive configurations.
CRUD/
├── controllers/
│ └── product.controllers.js # Handles business logic for product routes
├── db/
│ ├── connectdb.js # Database connection logic
│ └── queries.js # SQL queries for database operations
├── routes/
│ └── product.routes.js # API routes for products
├── .env # Environment variables
├── .gitignore # Ignored files for Git
├── package.json # Dependencies and scripts
├── package-lock.json # Exact dependency versions
└── server.js # Main server file
Ensure you have the following installed on your system:
Create a .env
file in the root of the project and add the following:
MYSQL_DATABASE_NAME="ecom"
MYSQL_PASSWORD=your_password
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PORT=3306
PORT=5000
Replace your_password
with your desired password.
git clone https://github.com/Varunyadavgithub/mysql-node-rest-api.git
npm install
Run the following command to start a MySQL container:
docker run --name sqldb -d -p 3306:3306 --rm -v mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=your_password mysql:latest
- Access the container's MySQL shell:
docker exec -it sqldb mysql -u root -p
- Create the database:
CREATE DATABASE ecom;
- Exit the MySQL shell:
exit
npm run dev
The API will start running at http://localhost:5000.
HTTP Method | Endpoint | Description |
---|---|---|
GET | /products |
Retrieve all products |
GET | /products/:id |
Retrieve a product by ID |
POST | /products |
Add a new product |
PUT | /products/:id |
Update a product by ID |
DELETE | /products/:id |
Delete a product by ID |
-
Stop MySQL Container:
docker stop sqldb
-
Rebuild Node Modules:
npm install
- Node.js
- Express.js
- MySQL
- Docker
- Nodemon (for development)