A comprehensive Express.js backend API for a modern e-commerce clothing store with full features including user authentication, product management, cart functionality, order processing, and admin panel.
- User Authentication & Authorization: JWT-based auth with role-based access control
- Product Management: Complete CRUD operations with variants, inventory tracking, and reviews
- Shopping Cart: Persistent cart with real-time inventory validation
- Order Processing: Full order lifecycle from creation to delivery tracking
- Category Management: Hierarchical category structure with parent-child relationships
- User Profiles: Customer profiles with multiple addresses and preferences
- Admin Dashboard: Comprehensive admin features for managing users, products, and orders
- Search & Filtering: Advanced product search with multiple filter options
- Review System: Product reviews and ratings with verification
- Analytics: Order analytics and user statistics
Backend:
- Node.js (Runtime Environment)
- Express.js (Web Framework)
- MongoDB (Database)
- Mongoose (ODM)
- JWT (Authentication)
- bcryptjs (Password Hashing)
Security & Validation:
- Helmet (Security Headers)
- Express Rate Limit (Rate Limiting)
- Express Validator (Input Validation)
- CORS (Cross-Origin Resource Sharing)
Development & Utilities:
- Nodemon (Development Server)
- Morgan (HTTP Logging)
- Compression (Response Compression)
- Multer (File Upload)
- Cloudinary (Image Storage)
- Stripe (Payment Processing)
Before running this application, make sure you have the following installed:
-
Clone the repository:
git clone https://github.com/your-username/clothing-store-backend.git cd clothing-store-backend -
Install dependencies:
npm install
-
Set up environment variables:
cp .env.example .env
Edit the
.envfile with your configuration values:PORT=5000 NODE_ENV=development MONGODB_URI=mongodb://localhost:27017/clothing-store JWT_SECRET=your_super_secret_jwt_key FRONTEND_URL=http://localhost:3000
-
Start MongoDB: Make sure MongoDB is running on your system or set up MongoDB Atlas
-
Seed the database with sample data:
npm run seed
Start the development server with auto-reload:
npm run devThe API will be available at http://localhost:5000
Start the production server:
npm startVisit http://localhost:5000/health to verify the API is running
http://localhost:5000/api
Most endpoints require authentication. Include the JWT token in the Authorization header:
Authorization: Bearer <your_jwt_token>
POST /register- Register a new userPOST /login- Login userPOST /logout- Logout userGET /me- Get current user profilePUT /me- Update user profilePOST /change-password- Change passwordPOST /forgot-password- Request password resetPOST /reset-password/:token- Reset passwordPOST /refresh-token- Refresh JWT token
GET /- Get all products (with filtering and pagination)GET /search- Search productsGET /featured- Get featured productsGET /:id- Get single productPOST /- Create product (Admin only)PUT /:id- Update product (Admin only)DELETE /:id- Delete product (Admin only)POST /:id/reviews- Add product reviewPUT /:id/reviews/:reviewId- Update reviewDELETE /:id/reviews/:reviewId- Delete review
GET /- Get all categoriesGET /tree- Get category tree structureGET /:id- Get single categoryGET /:id/products- Get products in categoryPOST /- Create category (Admin only)PUT /:id- Update category (Admin only)DELETE /:id- Delete category (Admin only)
GET /- Get user's cartPOST /items- Add item to cartPUT /items/:itemId- Update cart item quantityDELETE /items/:itemId- Remove item from cartDELETE /- Clear entire cartGET /summary- Get cart summary
GET /- Get user's orders (or all orders for admin)GET /:id- Get single orderPOST /- Create new orderPUT /:id/status- Update order status (Admin only)PUT /:id/shipping- Update shipping info (Admin only)POST /:id/cancel- Cancel orderGET /analytics/summary- Get order analytics (Admin only)GET /:id/invoice- Get order invoice
GET /- Get all users (Admin only)GET /:id- Get user by IDPUT /:id- Update userDELETE /:id- Deactivate user (Admin only)POST /:id/addresses- Add user addressPUT /:id/addresses/:addressId- Update user addressDELETE /:id/addresses/:addressId- Delete user addressGET /:id/orders- Get user's ordersGET /:id/stats- Get user statistics
Create a .env file in the root directory with the following variables:
# Server Configuration
PORT=5000
NODE_ENV=development
FRONTEND_URL=http://localhost:3000
# Database Configuration
MONGODB_URI=mongodb://localhost:27017/clothing-store
# JWT Configuration
JWT_SECRET=your_super_secret_jwt_key_here_change_in_production
JWT_EXPIRES_IN=7d
# Cloudinary Configuration (for image uploads)
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
# Stripe Configuration (for payments)
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret
# Email Configuration (using Nodemailer)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_app_password
# Admin Configuration
ADMIN_EMAIL=admin@clothingstore.com
ADMIN_PASSWORD=admin123Run the test suite:
npm testRun tests in watch mode:
npm run test:watchGenerate coverage report:
npm run test:coverageclothing-store-backend/
βββ models/ # Mongoose models
β βββ User.js # User model
β βββ Product.js # Product model
β βββ Category.js # Category model
β βββ Cart.js # Cart model
β βββ Order.js # Order model
βββ routes/ # API routes
β βββ auth.js # Authentication routes
β βββ products.js # Product routes
β βββ categories.js # Category routes
β βββ cart.js # Cart routes
β βββ orders.js # Order routes
β βββ users.js # User management routes
βββ middleware/ # Custom middleware
β βββ auth.js # Authentication middleware
β βββ errorHandler.js # Error handling middleware
β βββ validation.js # Input validation middleware
βββ scripts/ # Utility scripts
β βββ seedDatabase.js # Database seeding script
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore file
βββ package.json # Dependencies and scripts
βββ server.js # Main server file
βββ README.md # Project documentation
npm start- Start production servernpm run dev- Start development server with nodemonnpm run seed- Seed database with sample datanpm test- Run tests
This project follows:
- RESTful API design principles
- Clean code practices
- Proper error handling
- Input validation and sanitization
- Security best practices
-
Create a Heroku app:
heroku create your-app-name
-
Set environment variables:
heroku config:set NODE_ENV=production heroku config:set JWT_SECRET=your_jwt_secret heroku config:set MONGODB_URI=your_mongodb_atlas_uri
-
Deploy:
git push heroku main
# Build the image
docker build -t clothing-store-api .
# Run the container
docker run -p 5000:5000 --env-file .env clothing-store-api- Set up a server instance
- Install Node.js and MongoDB
- Clone the repository
- Install dependencies and set environment variables
- Use PM2 for process management:
npm install -g pm2 pm2 start server.js --name "clothing-store-api"
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name - Make your changes and commit them:
git commit -m 'Add some feature' - Push to the branch:
git push origin feature/your-feature-name - Submit a pull request
- Follow RESTful API conventions
- Write clean, readable code
- Add proper error handling
- Include input validation
- Add tests for new features
- Update documentation as needed
- Use meaningful commit messages
After running npm run seed, you can use these test accounts:
Admin Account:
- Email:
admin@clothingstore.com - Password:
admin123
Customer Account:
- Email:
john.doe@example.com - Password:
password123
- JWT-based authentication
- Password hashing with bcrypt
- Rate limiting to prevent abuse
- Input validation and sanitization
- CORS protection
- Security headers with Helmet
- Role-based access control
This project is licensed under the MIT License - see the LICENSE file for details.
- Express.js for the web framework
- MongoDB for the database
- Mongoose for elegant MongoDB object modeling
- JWT for secure authentication
- bcryptjs for password hashing
For support or questions about this project, please open an issue on GitHub.
Made with β€οΈ for modern e-commerce