How dvirhayat/web-course-backend works
Project Type and Purpose
• This is a full-stack Next.js application (React front end + serverless API) whose back end manages users, hobbies, and workplaces for the Social Network project. It uses Next.js’ app & pages folders to serve UI and API routes, Mongoose for ODM, and MongoDB (Atlas or self-hosted) as its data store.
File-Structure Analysis
• Top level:
• package.json / next.config.mjs / vercel.json – build, runtime, and deployment configs
• pages/api – Next.js API routes (login, register, userUtil, adminUtil, hobbiesUtil, workplaceUtil)
• src/app – Next.js 13+ React UI (globals.css, layout.js, page.js)
• lib – Data-access layer (mongodb connection, CRUD utilities for users/hobbies/workplaces)
• models – Mongoose schemas (User, Hobby, Workplace)
• Patterns:
• “Controller” layer in pages/api → “Service/DAO” layer in lib → “Model” definitions → MongoDB
• Separation of concerns: React (UI) vs. API endpoints (business logic) vs. DB utilities
Key Components
a. Client & Presentation Layer
• Browser / React UI components (src/app)
• Tailwind / CSS / static assets (public/, globals.css)
b. API Layer (Next.js Serverless Functions)
• pages/api/*.js endpoints (login, register, userUtil, adminUtil, hobbiesUtil, workplaceUtil)
c. Service/DAO Layer
• lib/mongodb.js (connection manager)
• lib/usersDB.js, hobbiesDB.js, workplaceDB.js (CRUD operations)
d. Data Models
• models/User.js, Hobby.js, Workplace.js (Mongoose schemas & validation)
e. Database
• MongoDB (Atlas or other), environment-driven URI
f. External Services & Infrastructure
• Vercel (hosting)
• Environment variables (.env.local → MONGODB_URI)
Relationships & Data Flows
• UI → HTTP call → API endpoint
• API endpoint → calls DAO functions in lib/*DB.js → interacts with Mongoose models
• Mongoose → issues queries to MongoDB → returns data to DAO → API → UI
• Admin endpoints perform DELETE/PATCH operations; Hobby and Workplace endpoints allow GET/PUT/DELETE
• next.config.mjs and vercel.json configure routing and rewrites between front end and API
Architectural Patterns & Principles
• Layered architecture (Presentation → Controller → Service/DAO → Data/Model → Persistence)
• Single responsibility per module: each API file handles one resource
• Reusable DB connection (caching via lib/mongodb.js)
• Convention-over-configuration (Next.js folder conventions)