A robust e-commerce backend built with Node.js, Express, MongoDB, and various third-party services using ES6 Modules.
- User Authentication: Registration, login, OTP verification, password reset
- Product Management: CRUD operations, image uploads, reviews, ratings
- Order Management: Order processing, payment integration, tracking, OTP delivery verification
- Admin Panel: Comprehensive admin features for managing products, orders, customers
- File Upload: Cloudinary integration for image management
- Email Service: Nodemailer integration for transactional emails
- Payment Gateway: Razorpay integration for secure payments
- JWT authentication with secure cookies
- Rate limiting and CORS protection
- Input validation and sanitization
- Password hashing with bcrypt
- Secure file upload with size limits
- Account lockout after failed login attempts
- Shopping cart management
- Wishlist functionality
- Address management
- Order tracking with delivery OTP
- Inventory management
- Customer segmentation
- Marketing campaigns (email/SMS)
- Analytics and reporting
- Node.js (v16.0.0 or higher) - ES6 Modules support required
- MongoDB (v4.4 or higher)
- npm or yarn package manager
-
Clone the repository
cd backend
-
Install dependencies
npm install
-
Environment Setup Copy the sample environment file and configure:
cp .env.sample .env
Then edit
.env
with your actual credentials:# Basic Configuration PORT=5000 NODE_ENV=development MONGODB_URI=mongodb://localhost:27017/hash-ecommerce JWT_SECRET=your-super-secret-jwt-key-at-least-64-characters-long # Third-party Services CLOUDINARY_CLOUD_NAME=your-cloudinary-cloud-name CLOUDINARY_API_KEY=your-cloudinary-api-key CLOUDINARY_API_SECRET=your-cloudinary-api-secret RAZORPAY_KEY_ID=rzp_test_xxxxxxxxxxxxxxxx RAZORPAY_KEY_SECRET=your-razorpay-key-secret EMAIL_USER=your-email@gmail.com EMAIL_PASS=your-16-digit-app-password FRONTEND_URL=http://localhost:5173 ADMIN_URL=http://localhost:5174
-
Start the server
# Development with auto-restart npm run dev # Production npm start
This project uses ES6 Modules instead of CommonJS:
- β
import/export
syntax - β
"type": "module"
in package.json - β
.js
file extensions in imports - β Modern JavaScript features
// OLD (CommonJS)
const express = require('express');
module.exports = router;
// NEW (ES6 Modules)
import express from 'express';
export default router;
http://localhost:5000/api
POST /api/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"phone": "+919876543210",
"password": "SecurePass123"
}
POST /api/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "SecurePass123"
}
POST /api/auth/verify-otp
Content-Type: application/json
{
"phone": "+919876543210",
"otp": "123456"
}
GET /api/auth/me
Authorization: Bearer <token>
POST /api/auth/resend-otp
- Resend OTPPOST /api/auth/forgot-password
- Password resetPATCH /api/auth/change-password
- Change passwordGET /api/auth/addresses
- Get user addressesPOST /api/auth/addresses
- Add new addressGET /api/auth/wishlist
- Get wishlistPOST /api/auth/wishlist/:productId
- Add to wishlist
backend/
βββ controllers/ # Route controllers (ES6 exports)
β βββ authController.js
β βββ productController.js
β βββ orderController.js
β βββ adminController.js
βββ middleware/ # Custom middleware
β βββ auth.js
β βββ upload.js
β βββ errorHandler.js
βββ models/ # MongoDB models
β βββ User.js
β βββ Product.js
β βββ Order.js
β βββ Admin.js
β βββ Campaign.js
βββ routes/ # API routes
β βββ authRoutes.js
β βββ productRoutes.js
β βββ orderRoutes.js
β βββ adminRoutes.js
βββ services/ # External services
β βββ emailService.js
β βββ paymentService.js
βββ utils/ # Utility functions
β βββ appError.js
β βββ catchAsync.js
βββ .env.sample # Environment variables template
βββ package.json # ES6 modules enabled
βββ server.js # Main server file
- Registration: User registers with email/phone
- OTP Verification: SMS OTP sent for phone verification
- Login: User logs in with verified credentials
- JWT Token: Secure token issued for authenticated requests
- Protected Routes: Token required for accessing protected endpoints
- Create payment orders
- Verify payment signatures
- Handle webhooks for payment status updates
- Support for multiple payment methods (cards, UPI, net banking)
- Create order with payment intent
- Redirect to Razorpay checkout
- Process payment with Razorpay
- Verify payment signature
- Update order status
- Welcome emails
- OTP verification
- Order confirmations
- Shipping notifications
- Password reset
- Marketing campaigns
- Automatic image optimization
- Multiple format support
- Responsive image URLs
- Secure upload with validations
- Authentication: JWT with secure HTTP-only cookies
- Authorization: Role-based access control
- Rate Limiting: API rate limiting to prevent abuse
- Input Validation: Comprehensive input validation
- CORS: Configured for specific origins
- Helmet: Security headers protection
- Password Security: Bcrypt hashing with salt
- Global Error Handler: Centralized error handling
- Custom Error Classes: Operational vs programming errors
- Validation Errors: Detailed validation error responses
- Development vs Production: Different error details based on environment
- Set up MongoDB (local or Atlas)
- Create accounts for third-party services:
- Cloudinary - Image management
- Razorpay - Payment gateway
- Configure Gmail App Password for emails
- Copy and edit
.env.sample
to.env
- Start development server:
npm run dev
{
"status": "success",
"message": "Operation completed successfully",
"data": {
"user": {...}
}
}
{
"status": "fail",
"message": "Validation failed",
"errors": [
{
"field": "email",
"message": "Please provide a valid email"
}
]
}
See .env.sample
for a complete list of required environment variables including:
- Database: MongoDB connection string
- Authentication: JWT secret
- File Upload: Cloudinary credentials
- Communications: Twilio & email settings
- Payments: Razorpay API keys
- URLs: Frontend and admin URLs
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new features
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License.
Hash Store Backend - Built with β€οΈ using modern ES6 modules for scalable e-commerce needs.