How userrounakk/kisanpedia-backend works
Project Type and Purpose
• This is a RESTful backend service (“Kisanpedia Backend”) built with Node.js, Express, and MongoDB.
• Purpose: expose CRUD APIs for five core domain entities (Location, Plant, Seller, Store, User), enforce authentication/authorization, and serve data to a frontend or other clients.
File Structure
• app.js: entry point—initializes Express, middleware, connects to MongoDB, mounts routers.
• app/controller: business-logic layer; one controller per entity.
• app/middleware: request-processing filters (auth checks, validation, file uploads).
• routes: Express routers mapping HTTP endpoints to controllers.
• models: Mongoose schemas representing domain entities.
• helpers: utility functions (e.g., JWT token generation).
• package.json/.env: configuration, dependencies, environment variables.
README for Insights
• Tech stack: Node.js, Express, MongoDB, Mongoose.
• Entities and relationships:
• Location: unique name
• Plant: description, image URL, availability (locations), price
• Seller: contact details, plants they sell, location
• Store: address, map location
• User: roles (Admin, Super Admin)
• Features: authentication, role-based access, entity management.
Main Components & Their Relationships
a. API Layer (Express Server)
• app.js loads global middleware (JSON parser, CORS, logging).
• Routes: /auth, /location, /plant, /seller, /store; each routed to its controller.
b. Middleware Layer
• auth.middleware: verifies JWT and user role.
• entity-specific middleware: e.g., plant.middleware validates incoming plant data.
• uploader.middleware: handles multipart file uploads for plant images.
c. Controller Layer
• auth.controller: login, signup, token refresh.
• X.controller: CRUD handlers for each domain model.
d. Data Layer
• Mongoose models (Location, Plant, Seller, Store, User) interact with MongoDB.
e. External Services
• MongoDB Atlas (or local instance).
• (Optionally) Object storage if images are stored externally.
f. Helpers
• token.helper: JWT creation and verification.
Architectural Patterns & Design Principles
• Layered Architecture: clear separation of concerns (Routing → Middleware → Controller → Data).
• Single Responsibility: each middleware and controller has a distinct purpose.
• Role-Based Access Control: enforce via auth.middleware.
• Configuration via environment variables (.env).
Final Recommendations
• Every component is labeled, and data flow is unambiguous.
• Related files under their layer names for clarity.
• Call out any environment-driven configurations (e.g., DB URI, JWT_SECRET) near the configuration file.
• Role checks (Admin vs Super Admin) in the auth middleware block.
• Review with a team member to confirm no major component is missing.