This is a backend API for an e-commerce platform that supports user registration, login, product management, and order management with JWT authentication, cron jobs for notifications, and proper error handling.
API, JWT, Authentication, Product Management, Order Management, Cron Jobs, Nodemailer.
- E-Commerce API
- Starting 🚀
- Pre-requirements 📋
- Installation 🔧
- Server setup
- Client setup
- API Endpoints 📡
- User Registration and Login
- Product Management
- Order Management
- Cron Jobs ⏰
- Built with 🛠️
Before starting, make sure you have the following installed:
- Clone this repository
git clone https://github.com/your-repository/e-commerce-api.git- Navigate to the project directory
cd e-commerce-api- Install dependencies
npm install- Create a .env file Copy the example environment file and modify it with your credentials.
cp .env.example .env- Update .env file with your credentials:
# Server Configuration
PORT=3000
# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017/e-commerce
# JWT Configuration
JWT_SECRET=your-secret-key-here
# Email Configuration (for nodemailer)
EMAIL_FROM=your-email@example.com
ADMIN_EMAIL=admin@example.com
NODE_ENV=production
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_USER=your-email@example.com
SMTP_PASS=your-email-passwordIf your project includes a client, navigate to the client folder and set it up:
cd client
npm install
cp .env.example .env.localIn the client's .env.local file, set the API URL:
REACT_APP_API_URL="http://localhost:3000/api"- Register User
- Method:
POST - URL:
/api/auth/register - Body:
- Method:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}- Response:
{
"message": "User registered successfully"
}- Login User
- Method:
POST - URL:
/api/auth/login - Body:
- Method:
{
"email": "john@example.com",
"password": "password123"
}- Response:
{
"token": "JWT_AUTH_TOKEN"
}- Create Product
- Method:
POST - URL:
/api/products - Body:
- Method:
{
"name": "Product Name",
"description": "Product description",
"price": 20.5,
"stock": 100
}- Response:
{
"message": "Product created successfully"
}- Update Product
- Method:
PUT - URL:
/api/products/:id - Body:
- Method:
{
"price": 25.0,
"stock": 50
}- Response:
{
"message": "Product updated successfully"
}- Get All Products
- Method:
GET - URL:
/api/products - Query Params:
?page=1&limit=10&minPrice=10&maxPrice=100&search=product - Response:
- Method:
{
"products": [
{
"name": "Product Name",
"description": "Product description",
"price": 20.5,
"stock": 100
}
],
"pagination": {
"currentPage": 1,
"totalPages": 5
}
}- Delete Product
- Method:
DELETE - URL:
/api/products/:id - Response:
- Method:
{
"message": "Product soft-deleted successfully"
}- Create Order
- Method:
POST - URL:
/api/orders - Body:
- Method:
{
"user": "userId",
"products": [
{
"productId": "productId",
"quantity": 2
}
]
}- Response:
{
"message": "Order created successfully"
}- Get All Orders for a User
- Method:
GET - URL:
/api/orders/user/:userId - Response:
- Method:
{
"orders": [
{
"orderId": "12345",
"status": "Pending",
"totalPrice": 50.0
}
]
}- Update Order Status
- Method:
PUT - URL:
/api/orders/:id - Body:
- Method:
{
"status": "Shipped"
}- Response:
{
"message": "Order status updated"
}- Delete Order
- Method:
DELETE - URL:
/api/orders/:id - Response:
- Method:
{
"message": "Order deleted successfully"
}- Product Stock Monitoring Cron:
- Frequency: Runs daily at midnight
- Functionality: Notifies the admin if a product's stock falls below 10
- Example: Email notification sent using Nodemailer
- Order Fulfillment Reminder Cron:
- Frequency: Runs every hour
- Functionality: Sends email reminders for pending orders older than 24 hours
- Node.js - JavaScript runtime environment
- Express.js - Backend framework
- MongoDB - NoSQL database
- Mongoose - MongoDB object modeling
- Nodemailer - For sending emails
- JWT - For authentication
- Cron - For scheduling tasks
- Joi - For validation