A robust and feature-rich RESTful API for e-commerce applications built with Node.js, Express, and MongoDB.
π Live API - Experience the API in action
π API Documentation - Interactive documentation and testing interface
π Postman Collection - Ready-to-use Postman collection
Try the API instantly:
# Get all products
curl https://ecommerce-rest-api-bmw6.onrender.com/api/v1/products
# Register a new user
curl -X POST https://ecommerce-rest-api-bmw6.onrender.com/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"name":"Test User","email":"test@example.com","password":"password123"}'
- π Authentication & Authorization - JWT-based auth with secure cookie handling
- π₯ User Management - Registration, login, profile management, and role-based access
- π¦ Product Management - CRUD operations for products with image upload
- β Review System - Users can rate and review products
- ποΈ Order Processing - Complete order management with status tracking
- π Security - Rate limiting, XSS protection, MongoDB injection prevention
- π File Upload - Secure image upload for product images
- π Data Validation - Comprehensive input validation with Joi
- π Error Handling - Centralized error handling with custom error classes
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Authentication: JSON Web Tokens (JWT)
- Validation: Joi
- Security: Helmet, CORS, XSS Protection, Rate Limiting
- File Upload: Express File Upload
- Email: Nodemailer
- Development: Nodemon
Before running this project, make sure you have the following installed:
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
- npm or yarn
π‘ Want to test immediately? Check out our live demo or download the Postman collection
git clone https://github.com/CodingWithSanjeet/ecommerce-rest-api.git
cd ecommerce-rest-api
npm install
Create a .env
file in the root directory:
# Database
MONGO_URI=mongodb://localhost:27017/ecom-api
# JWT
JWT_SECRET=your-super-secret-jwt-key
JWT_LIFETIME=30d
# Server
PORT=3000
NODE_ENV=development
# Email Configuration (Optional)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password
npm start
The API will be running at http://localhost:3000
Local Development:
http://localhost:3000/api/v1
Live Production:
https://your-app-name.onrender.com/api/v1
Method | Endpoint | Description |
---|---|---|
POST | /auth/register |
Register a new user |
POST | /auth/login |
Login user |
POST | /auth/logout |
Logout user |
POST | /auth/forgot-password |
Request password reset |
POST | /auth/reset-password |
Reset password |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /users |
Get all users (Admin only) | β |
GET | /users/profile |
Get current user profile | β |
PATCH | /users/profile |
Update user profile | β |
PATCH | /users/change-password |
Change password | β |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /products |
Get all products | β |
GET | /products/:id |
Get single product | β |
POST | /products |
Create product (Admin only) | β |
PATCH | /products/:id |
Update product (Admin only) | β |
DELETE | /products/:id |
Delete product (Admin only) | β |
POST | /products/upload-image |
Upload product image | β |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /reviews |
Get all reviews | β |
GET | /reviews/:id |
Get single review | β |
POST | /reviews |
Create review | β |
PATCH | /reviews/:id |
Update review (Owner only) | β |
DELETE | /reviews/:id |
Delete review (Owner only) | β |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /orders |
Get all orders (Admin only) | β |
GET | /orders/my-orders |
Get current user orders | β |
GET | /orders/:id |
Get single order | β |
POST | /orders |
Create order | β |
PATCH | /orders/:id |
Update order | β |
POST /api/v1/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}
POST /api/v1/products
Authorization: Bearer <your-jwt-token>
Content-Type: application/json
{
"name": "Wireless Headphones",
"price": 99.99,
"description": "High-quality wireless headphones",
"category": "electronics",
"company": "techcorp",
"colors": ["black", "white"],
"featured": true,
"freeShipping": true,
"inventory": 50
}
POST /api/v1/orders
Authorization: Bearer <your-jwt-token>
Content-Type: application/json
{
"tax": 8.99,
"shippingFee": 5.99,
"orderItems": [
{
"name": "Wireless Headphones",
"image": "/uploads/headphones.jpg",
"price": 99.99,
"amount": 2,
"product": "64f8a9b2c1d2e3f4a5b6c7d8"
}
]
}
ecom-api/
βββ controller/ # Route controllers
β βββ authController.js
β βββ userController.js
β βββ productController.js
β βββ reviewController.js
β βββ orderController.js
βββ models/ # Mongoose models
β βββ User.js
β βββ Product.js
β βββ Review.js
β βββ Order.js
βββ routes/ # API routes
β βββ authRoutes.js
β βββ userRoutes.js
β βββ productRoutes.js
β βββ reviewRoutes.js
β βββ orderRoutes.js
βββ middleware/ # Custom middleware
β βββ authentication.js
β βββ error-handler.js
β βββ not-found.js
βββ errors/ # Custom error classes
β βββ AppError.js
β βββ BadRequestError.js
β βββ NotFoundError.js
β βββ UnauthenticatedError.js
β βββ UnauthorizedError.js
βββ utils/ # Utility functions
β βββ asyncWrapper.js
β βββ jwt.js
β βββ index.js
βββ db/ # Database connection
β βββ connect.js
βββ public/ # Static files
β βββ uploads/ # Uploaded images
βββ app.js # App configuration
βββ package.json
βββ README.md
- Rate Limiting: Prevents brute force attacks
- Helmet: Sets various HTTP security headers
- XSS Protection: Prevents cross-site scripting attacks
- MongoDB Injection: Sanitizes user input
- CORS: Configures cross-origin resource sharing
- JWT: Secure token-based authentication
- Password Hashing: Uses bcrypt for password security
# Run tests (if implemented)
npm test
# Run tests in watch mode
npm run test:watch
NODE_ENV=production
MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/ecom-api
JWT_SECRET=your-super-secure-production-secret
PORT=3000
-
Connect Repository
- Go to Render Dashboard
- Click "New +" β "Web Service"
- Connect your GitHub repository
-
Configure Settings
- Name: your-app-name
- Environment: Node
- Build Command:
npm install
- Start Command:
npm start
-
Set Environment Variables
NODE_ENV=production MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/ecom-api JWT_SECRET=your-super-secure-production-secret
-
Deploy
- Click "Create Web Service"
- Render will automatically deploy your app
- Your API will be available at
https://your-app-name.onrender.com
# Login to Heroku
heroku login
# Create Heroku app
heroku create your-app-name
# Set environment variables
heroku config:set MONGO_URI=your-mongo-uri
heroku config:set JWT_SECRET=your-jwt-secret
# Deploy
git push heroku main
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Your Name
- GitHub: @CodingWithSanjeet
- LinkedIn: Sanjeet Kumar
- Email: sanjeet.kumar.nitt@gmail.com
- Express.js - Fast, unopinionated, minimalist web framework
- MongoDB - NoSQL database
- Mongoose - MongoDB object modeling for Node.js
- JWT - JSON Web Tokens for authentication