How eyad-abdelmohsen/social-media works
Project type and purpose
• This is a monolithic RESTful backend API for a social-media platform, implemented in Node.js/Express with TypeScript.
• It provides user management (signup/login), post/comment/like operations, and friend-request workflows, persisting data in MongoDB and storing uploads on the file system.
File structure
• Top level
• package.json, tsconfig.json, src/, dist/, uploads/
• src/ (and dist/ mirror)
• controllers/ – Express route handlers (friend.controller, post.controller, user.controller)
• routes/ – Express routers mapping HTTP verbs & paths to controllers
• services/ – Business-logic layer (user.service, post.service, friends.service, etc.)
• models/ – Mongoose schemas/models (User, Post, Comment)
• middlewares/ – Authentication, authorization, validation, pagination, error-handling
• utils/ – JWT generation, custom request typings, role definitions, Multer setup
• errors/ – ApiError class for centralized error constructs
• validators/ – Input-validation logic (e.g., user.validator)
• index.ts – Express app bootstrap and middleware wiring
Key architectural patterns & design principles
• Layered (MVC + Service layer)
• Single responsibility per module (controllers orchestrate, services contain logic, models interact with DB)
• Centralized error handling & request validation via middleware
• Token-based auth (JWT)
• File-upload handling (Multer)
Main components
a. Client (Web/Mobile)
b. Load balancer/API Gateway (optional external Nginx or AWS ALB)
c. Express API server
• Middlewares
– Authentication (verifyToken)
– Authorization (allowedTo, isOwner, isIFrend)
– Validation & pagination
– Error & “not found” handlers
• Routing layer (UserRoute, PostRoute, FriendRoute)
• Controller layer (handles HTTP request/response)
• Service layer (business logic, coordinating models & utilities)
• Data access layer (Mongoose Models interacting with MongoDB)
• Utility modules (JWT, Multer, custom request enhancements)
d. MongoDB cluster
e. File storage (local uploads folder or S3 if extended)
f. External services (e.g., email/SMS, if added later)
Relationships and data flows
• Error flows: Controller/Service → throw ApiError → Error Middleware → Client