Skip to content

Backend

mhbarshan edited this page Apr 19, 2025 · 1 revision

πŸ› οΈ Backend Overview

🚧 Tech Stack

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB (Mongoose ODM)
  • Authentication: JWT + bcrypt + OTP (email-based)
  • Payments: Stripe API
  • File Upload: Multer
  • Environment Config: dotenv
  • Deployment: Render

πŸ“ Project Structure

backend/
β”œβ”€β”€ controllers/         # Route logic (e.g., userController, productController)
β”œβ”€β”€ models/              # Mongoose schemas
β”œβ”€β”€ routes/              # API route definitions
β”œβ”€β”€ middlewares/         # Auth, error handling, etc.
β”œβ”€β”€ utils/               # Helper functions (e.g., email sender, OTP generator)
β”œβ”€β”€ config/              # DB connection, Stripe config
β”œβ”€β”€ .env                 # Environment variables
β”œβ”€β”€ server.js            # Entry point
└── package.json

πŸ” Authentication Flow

  • Users register/login with email and password.
  • Passwords are hashed using bcrypt.
  • JWT is generated on login and sent to frontend.
  • Admin-only APIs are protected via middleware.
  • OTP verification is used for added security or password reset.

πŸ’Ό Admin Functionality

Admins can:

  • Add, update, or delete products
  • View and process orders

Protected by adminMiddleware.


πŸ›’ Key Modules

πŸ§‘β€πŸ’» User Module

  • Register / Login
  • Get Profile
  • Update Profile
  • OTP verification
  • JWT Authentication

πŸ“¦ Product Module

  • CRUD for products
  • Reviews system
  • Category/tags/filters

πŸ“¬ Order Module

  • Create order
  • View order history
  • Stripe payment integration

πŸ’³ Stripe Integration

  • POST /create-checkout-session β†’ Stripe checkout
  • Webhook listens for payment success

πŸ” Middleware

  • authMiddleware – verifies JWT
  • adminMiddleware – checks admin role
  • multer – upload image management

🌍 API Documentation

Base URL:

http://localhost:5000/api

Example Endpoints:

POST   /api/users/register
POST   /api/users/login
GET    /api/users/profile
POST   /api/products
GET    /api/products/:id
POST   /api/orders
POST   /api/payments/checkout

πŸš€ Running Locally

  1. Clone the repo:
git clone https://github.com/Learnathon-By-Geeky-Solutions/codeclusters
cd codeclusters/backend
  1. Install dependencies:
npm install
  1. Create .env file:
PORT = your_port
MONGODB_URI ="your mongoDb uri"
JWT_SECRET = "Provide_secret"
EMAIL_USER = "example@mail.com"
EMAIL_PASS = "password"
ADMIN_EMAIL = "example@mail.com"
ADMIN_PASSWORD = "password"
STRIPE_SECRET_KEY ='your_stripe_secret_key'
  1. Start the server:
npm run dev

πŸ” Available Scripts

npm run server      # Run with nodemon
npm start         # Run in production

πŸ› οΈ Deployment

Make sure to:

  • Set up environment variables in your hosting platform
  • Use build folder from frontend in server.js for production
  • Add a health check route (e.g. /api/health)

πŸ“Œ Tips

  • Keep tokens/keys in .env, never push them.
  • Use try-catch and async/await for clean error handling.
  • Keep controller logic modular and small.
  • Modularize routes and keep them RESTful.

βœ… Example .env

PORT = your_port
MONGODB_URI ="your mongoDb uri"
JWT_SECRET = "Provide_secret"
EMAIL_USER = "example@mail.com"
EMAIL_PASS = "password"
ADMIN_EMAIL = "example@mail.com"
ADMIN_PASSWORD = "password"
STRIPE_SECRET_KEY ='your_stripe_secret_key'