Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

62 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Absolutely. This is actually a great habit. Before starting the frontend tomorrow, it's worth making sure you understand why you built each backend component, not just how.


🌱 EcoLife Backend Summary

🎯 Goal

Build a production-ready REST API that allows users to:

  • Register/Login
  • Upload waste images
  • Store images on Cloudinary
  • Analyze images using Gemini AI
  • Save scan history
  • View dashboard statistics
  • Manage their profile

πŸ“‚ Overall Architecture

Client (React)
       β”‚
       β”‚ HTTP Request
       β–Ό
Express Server
       β”‚
       β”œβ”€β”€ Routes
       β”‚
       β–Ό
Controllers
       β”‚
       β”œβ”€β”€ Validation
       β”œβ”€β”€ Authentication
       β”œβ”€β”€ Business Logic
       β”‚
       β–Ό
Services
       β”‚
       β”œβ”€β”€ Cloudinary
       └── Gemini AI
       β”‚
       β–Ό
MongoDB

1️⃣ Express

Purpose

Acts as the backend server.

It:

  • Receives requests
  • Sends responses
  • Connects everything

Example:

POST /api/auth/login

↓

Express receives it

↓

Sends it to

loginController()

2️⃣ MongoDB + Mongoose

Purpose:

Store data.

Collections:

Users

Scans

User Example

{
  "name":"Srujan",
  "email":"abc@gmail.com",
  "password":"hashed"
}

Scan Example

{
  "user":"ObjectId",
  "imageUrl":"cloudinary url",
  "wasteType":"Plastic Bottle",
  "ecoScore":85
}

Mongoose creates schemas so every document has the correct structure.


3️⃣ Authentication (JWT)

Flow

Register

↓

Password hashed using bcrypt

↓

Saved in MongoDB


Login

↓

Check email

↓

Compare password

↓

Generate JWT

↓

Return JWT

Every protected request

↓

JWT

↓

authMiddleware

↓

Verify token

↓

req.user

Now every controller knows who is making the request.


4️⃣ bcrypt

Purpose

Never save passwords.

Instead of

password123

Database stores

$2b$10$afkdhfkj...

During login

bcrypt.compare()

checks the password.


5️⃣ JWT

Creates a signed token.

Example

Header

Payload

Signature

The client stores the token.

Every request

Authorization

Bearer TOKEN

Middleware verifies it.

If valid

req.user

becomes available.


6️⃣ Middleware

Middleware runs before controllers.

Flow

Request

↓

Middleware

↓

Controller

Examples

authMiddleware

Checks JWT.


uploadMiddleware

Uses multer.

Stores image inside memory.


validationMiddleware

Uses Joi.

Stops invalid requests.


errorMiddleware

Catches every error.

Instead of crashing

Returns JSON.


rateLimiter

Stops spam.

Example

100 requests

↓

Blocked

Helmet

Adds security headers.

Protects Express.


Compression

Compresses responses.

Smaller JSON.

Faster API.


Morgan

Logs requests.

Example

POST /login

200

38ms

7️⃣ Multer

Uploads images.

Instead of saving locally

uploads/

We used

memoryStorage()

Image becomes

req.file.buffer

Then directly sent to Cloudinary.


8️⃣ Cloudinary

Purpose

Image storage.

Flow

Image

↓

Buffer

↓

Cloudinary

↓

Image URL

↓

MongoDB

Database never stores images.

Only URL.


9️⃣ Gemini AI

Flow

Cloudinary URL

↓

Gemini Vision

↓

Analysis

↓

JSON

↓

Database

Returns

Waste Type

Category

Confidence

Eco Score

Recycling Instructions

Environmental Impact

πŸ”Ÿ Services

Controllers should not contain external API logic.

So we created

cloudinaryService

geminiService

Controller simply calls

uploadToCloudinary()

analyzeWaste()

Very clean architecture.


1️⃣1️⃣ Controllers

Controllers contain business logic.

Example

Upload Image

↓

Cloudinary

↓

Gemini

↓

Save Scan

↓

Return Response

Controllers never talk directly to UI.


1️⃣2️⃣ Routes

Routes map URLs.

Example

POST

/api/auth/login

↓

loginController

Routes are just traffic managers.


1️⃣3️⃣ Validation

Instead of

email="abc"

accepting anything,

Joi checks

Email

Password

Required Fields

before controller executes.

Cleaner.

Safer.


1️⃣4️⃣ Dashboard

Instead of returning every scan,

MongoDB aggregates.

Returns

Total Scans

Recyclable

Non-Recyclable

Average Eco Score

Recent Scans

One request.

Very efficient.


1️⃣5️⃣ Swagger

Purpose

API Documentation.

Instead of opening Postman,

Developers visit

/api-docs

and can test APIs directly.

Professional touch.


1️⃣6️⃣ Error Handling

Instead of

try

catch

inside every controller,

we created

asyncHandler

↓

errorMiddleware

Every error goes to one place.

Cleaner code.


πŸ“‚ Final Backend Structure

src/
β”‚
β”œβ”€β”€ config/
β”‚     mongodb
β”‚     cloudinary
β”‚
β”œβ”€β”€ controllers/
β”‚
β”œβ”€β”€ middleware/
β”‚
β”œβ”€β”€ models/
β”‚
β”œβ”€β”€ routes/
β”‚
β”œβ”€β”€ services/
β”‚
β”œβ”€β”€ utils/
β”‚
β”œβ”€β”€ validations/
β”‚
β”œβ”€β”€ docs/
β”‚
β”œβ”€β”€ app.js
β”‚
└── server.js

Every folder has one responsibility, which is exactly how production backends are organized.


🌟 Biggest Lessons You Learned

  • How authentication really works (JWT + bcrypt)
  • Why middleware exists and how it fits into the request lifecycle
  • How file uploads work using Multer and in-memory buffers
  • How to integrate third-party services like Cloudinary and Gemini
  • Why services separate external API logic from controllers
  • How to design REST APIs with clear routes and controllers
  • How to validate input before business logic
  • How to centralize error handling
  • How to secure an Express application with Helmet, rate limiting, and compression
  • How to document APIs with Swagger

πŸš€ Tomorrow's Plan: React Frontend

Tomorrow we'll start the frontend from scratch with the same mindset we used for the backendβ€”building it like a production application, not just making it work.

We'll cover:

  1. React architecture and folder structure
  2. Tailwind CSS setup
  3. React Router
  4. Authentication flow with JWT
  5. Axios configuration
  6. Protected routes
  7. Dashboard UI
  8. AI Scanner page
  9. Scan History
  10. Profile & Settings
  11. Responsive design and animations
  12. Deployment to Vercel

By the end, you'll have a complete AI-powered MERN application with a backend you understand and a frontend built using modern React practices.

You've made excellent progress todayβ€”especially debugging the Cloudinary issue yourself. That kind of debugging experience is what really builds confidence as a developer. Tomorrow we'll turn all of this backend functionality into a polished user experience. πŸš€

About

I am Srujan Poojari, a Computer Engineering student and aspiring AI Engineer passionate about building intelligent web and mobile applications that solve real-world problems. With experience in MERN Stack and React Native development, I enjoy creating scalable and user-friendly solutions powered by Artificial Intelligence. My interests include Gene

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages