src/ prefix from main entry and script commands.
T-shirt eCommerce Backend API
A RESTful API for a T-shirt eCommerce platform built with Node.js, Express, and MongoDB.
Features
- User authentication with JWT
- Product management (CRUD operations)
- Shopping cart functionality
- Order management
- Image upload for products
- Role-based access control (Admin/User)
Tech Stack
- Node.js
- Express.js
- MongoDB with Mongoose
- JWT for authentication
- Multer for file uploads
- Stripe for payments (optional)
Prerequisites
- Node.js (v14 or higher)
- MongoDB
- npm or yarn
Installation
- Clone the repository:
git clone <repository-url>
cd tshirt-ecommerce-backend
- Install dependencies:
npm install
- Create a
.envfile in the root directory with the following variables:
PORT=5000
MONGODB_URI=mongodb://localhost:27017/tshirt-ecommerce
JWT_SECRET=your_jwt_secret_key_here
JWT_EXPIRE=30d
STRIPE_SECRET_KEY=your_stripe_secret_key
UPLOAD_PATH=uploads
- Create an
uploadsdirectory in the root folder:
mkdir uploads
- Start the server:
# Development
npm run dev
# Production
npm start
API Endpoints
Authentication
POST /api/auth/register- Register a new userPOST /api/auth/login- Login userGET /api/auth/me- Get current user
Products
GET /api/products- Get all products (with filters)GET /api/products/:id- Get single productPOST /api/products- Create new product (Admin only)PUT /api/products/:id- Update product (Admin only)DELETE /api/products/:id- Delete product (Admin only)
Cart
GET /api/cart- Get user's cartPOST /api/cart- Add item to cartPUT /api/cart/:itemId- Update cart item quantityDELETE /api/cart/:itemId- Remove item from cartDELETE /api/cart- Clear cart
Orders
POST /api/orders- Create new orderGET /api/orders- Get all orders (Admin only)GET /api/orders/myorders- Get user's ordersGET /api/orders/:id- Get single orderPUT /api/orders/:id/status- Update order status (Admin only)PUT /api/orders/:id/pay- Update order payment status
Request/Response Examples
Register User
POST /api/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}
Create Product
POST /api/products
Content-Type: multipart/form-data
Authorization: Bearer <token>
{
"name": "Classic White T-shirt",
"description": "Premium cotton t-shirt",
"price": 29.99,
"sizes": ["S", "M", "L", "XL"],
"colors": ["White", "Black"],
"category": "men",
"stock": 100
}
Add to Cart
POST /api/cart
Content-Type: application/json
Authorization: Bearer <token>
{
"productId": "product_id_here",
"quantity": 2,
"size": "M",
"color": "White"
}
Error Handling
The API uses a consistent error response format:
{
"success": false,
"error": "Error message here"
}
Security
- JWT-based authentication
- Password hashing with bcrypt
- Role-based access control
- Input validation
- File upload restrictions
Contributing
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
Description
Languages
JavaScript
100%