2025-12-11 22:00:27 +05:30

169 lines
3.2 KiB
Markdown

# 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
1. Clone the repository:
```bash
git clone <repository-url>
cd tshirt-ecommerce-backend
```
2. Install dependencies:
```bash
npm install
```
3. Create a `.env` file 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
```
4. Create an `uploads` directory in the root folder:
```bash
mkdir uploads
```
5. Start the server:
```bash
# Development
npm run dev
# Production
npm start
```
## API Endpoints
### Authentication
- `POST /api/auth/register` - Register a new user
- `POST /api/auth/login` - Login user
- `GET /api/auth/me` - Get current user
### Products
- `GET /api/products` - Get all products (with filters)
- `GET /api/products/:id` - Get single product
- `POST /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 cart
- `POST /api/cart` - Add item to cart
- `PUT /api/cart/:itemId` - Update cart item quantity
- `DELETE /api/cart/:itemId` - Remove item from cart
- `DELETE /api/cart` - Clear cart
### Orders
- `POST /api/orders` - Create new order
- `GET /api/orders` - Get all orders (Admin only)
- `GET /api/orders/myorders` - Get user's orders
- `GET /api/orders/:id` - Get single order
- `PUT /api/orders/:id/status` - Update order status (Admin only)
- `PUT /api/orders/:id/pay` - Update order payment status
## Request/Response Examples
### Register User
```http
POST /api/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}
```
### Create Product
```http
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
```http
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:
```json
{
"success": false,
"error": "Error message here"
}
```
## Security
- JWT-based authentication
- Password hashing with bcrypt
- Role-based access control
- Input validation
- File upload restrictions
## Contributing
1. Fork the repository
2. Create your feature branch
3. Commit your changes
4. Push to the branch
5. Create a new Pull Request