A production-ready REST API for an e-commerce platform built with Node.js, Express, and MySQL (raw queries with mysql2).
Ensure MySQL is running, then configure your connection parameters in the .env file:
PORT=5000
DB_HOST=localhost
DB_USER=your_mysql_user
DB_PASSWORD=your_mysql_password
DB_NAME=ecommerce_db
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=30d
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secretInstall dependencies:
npm installStart the development server using nodemon:
npm run devFor production deployment:
npm start- Method:
POST - URL:
/api/auth/register - JSON Body:
{
"first_name": "Alice",
"last_name": "Smith",
"email": "alice@example.com",
"password": "securepassword123",
"phone": "555-0199",
"role": "customer"
}- Method:
POST - URL:
/api/auth/login - JSON Body:
{
"email": "alice@example.com",
"password": "securepassword123"
}- Method:
GET - URL:
/api/auth/me - Headers:
Authorization: Bearer <token>
- Method:
PUT - URL:
/api/users/profile - Headers:
Authorization: Bearer <token> - JSON Body:
{
"first_name": "Alicia",
"last_name": "Smith-Jones",
"phone": "555-9999"
}- Method:
PUT - URL:
/api/users/change-password - Headers:
Authorization: Bearer <token> - JSON Body:
{
"current_password": "securepassword123",
"new_password": "newsupersecure123"
}- Method:
POST - URL:
/api/categories - Headers:
Authorization: Bearer <token_of_admin> - JSON Body:
{
"name": "Home Appliances"
}- Method:
GET - URL:
/api/categories
- Method:
PUT - URL:
/api/categories/:id - Headers:
Authorization: Bearer <token_of_admin> - JSON Body:
{
"name": "Smart Home Appliances"
}- Method:
DELETE - URL:
/api/categories/:id - Headers:
Authorization: Bearer <token_of_admin>
- Method:
POST - URL:
/api/products - Headers:
Authorization: Bearer <token_of_admin> - Body Type:
form-data(Multipart Form)
| Key | Type | Value | Description |
|---|---|---|---|
name |
Text | Smart Coffee Maker |
Product Name |
price |
Text | 89.99 |
Product Price |
stock |
Text | 45 |
Quantity in Stock |
category_id |
Text | 1 |
ID of Category |
description |
Text | WiFi enabled espresso and coffee brewer. |
Description |
images |
File | [Select image files] |
Upload up to 5 files |
- Method:
GET - URL:
/api/products - Query Parameters:
page: Page index (default: 1)limit: Page count (default: 10)search: Filter by name/description (e.g.?search=iphone)category: Filter by category ID (e.g.?category=2)minPrice: Minimum price range (e.g.?minPrice=100)maxPrice: Maximum price range (e.g.?maxPrice=1000)sort: Sort criteria (price_asc,price_desc,newest)
- Method:
PUT - URL:
/api/products/:id - Headers:
Authorization: Bearer <token_of_admin> - JSON Body:
{
"name": "Smart Coffee Maker v2",
"price": 99.99,
"stock": 40,
"description": "Upgraded WiFi coffee maker with timer options."
}- Method:
DELETE - URL:
/api/products/:id - Headers:
Authorization: Bearer <token_of_admin>
- Method:
POST - URL:
/api/products/:id/images - Headers:
Authorization: Bearer <token_of_admin> - Body Type:
form-data(images: File array)
- Method:
PUT - URL:
/api/products/:id/images/:imageId - Headers:
Authorization: Bearer <token_of_admin> - Body Type:
form-data(image: Single File)
- Method:
DELETE - URL:
/api/products/:id/images/:imageId - Headers:
Authorization: Bearer <token_of_admin>
- Method:
POST - URL:
/api/cart - Headers:
Authorization: Bearer <token> - JSON Body:
{
"product_id": 1,
"quantity": 2
}- Method:
PUT - URL:
/api/cart/items/:productId - Headers:
Authorization: Bearer <token> - JSON Body:
{
"quantity": 5
}- Method:
DELETE - URL:
/api/cart/items/:productId - Headers:
Authorization: Bearer <token>
- Method:
DELETE - URL:
/api/cart - Headers:
Authorization: Bearer <token>
- Method:
POST - URL:
/api/orders - Headers:
Authorization: Bearer <token> - Description: Submits the order from the current items inside the shopping cart.
- Method:
PUT - URL:
/api/orders/:id/status - Headers:
Authorization: Bearer <token_of_admin> - JSON Body:
{
"status": "Processing"
}(Valid statuses: Pending, Processing, Shipped, Delivered, Cancelled)