How masterjii/codesfortomorrow works
Project type and purpose
• This is a backend-only full-stack service (no UI in this repo) exposing REST endpoints and WebSocket endpoints.
• Purpose: provide authenticated CRUD operations on “resources” with single-session enforcement, role-based authorization, input validation, error handling, caching, and real-time notifications to clients.
File structure
• Top-level:
• index.js – entry point creating HTTP server + Socket.IO server
• src/app.js – Express app setup (middlewares, routes)
• src/config/config.js – environment and feature flags (e.g., Redis URL)
• src/controllers/*.js – business logic for auth & resources
• src/routes/*.js – Express route definitions
• src/db/mongoose.js – MongoDB connection via Mongoose
• src/models/*.js – Mongoose schema definitions (User, Resource)
• src/middleware/*.js – request validation, authentication (JWT via cookies), role-based authorization, error handling
• src/sockets/socket.js – Socket.IO configuration and event broadcasting
• src/utils/jwt.utils.js – JWT creation/verification helpers
README for additional insights
• Tech stack: Node.js, Express, MongoDB(+Mongoose), Socket.IO, Redis (optional), JWT.
• Key features: single-session enforcement, caching, role-based auth, real-time “resourceUpdated” events.
• Endpoints: POST /register, POST /login, GET /profile, POST /createResources, GET /getResources, PUT /updateResources/:id; WebSocket: connect with token & room.
Main components
a. Clients
• REST clients (e.g., Postman, frontend apps)
• WebSocket clients (Socket.IO)
b. API Layer
• HTTP Server (Express)
• Route Handlers (auth.routes, resource.routes)
• Middlewares (validation, auth, role check, error handling)
• Controllers (auth.controller, resource.controller)
c. Business/Service Layer
• JWT Utility (jwt.utils) – issue/verify tokens, enforce single session
• Cache Layer (Redis) – optional caching of resource queries
• Real-time Layer (Socket.IO) – authentication on connect, join rooms, broadcast updates
d. Data Layer
• MongoDB (Mongoose) – User and Resource collections
• Mongoose connection manager
e. Configuration
• config.js (environment, secrets)
Relationships and interactions
1) REST flow:
Client → HTTP Server → Validation Middleware → Auth Middleware → Controller → Service/Model → MongoDB → Controller → HTTP Response
• Controllers may read/write cache (Redis) before/after DB calls.
2) WebSocket flow:
Client(with JWT) → Socket.IO Server → Auth check on handshake → Client joins “resource_<id>” room → When resource updated via REST, Controller emits “resourceUpdated” on that room → Socket.IO pushes event to connected clients.
3) Single-session enforcement: upon login Controller generates JWT and stores session identifier in User document or Redis; on new login, invalidates previous.
Architectural patterns & design principles
• Layered architecture: Presentation (Routes/Middlewares) → Business (Controllers/Services) → Data (Models/DB)
• Separation of concerns: authentication, validation, error handling as distinct middlewares
• Pub/Sub over WebSocket rooms for real-time notifications
• Single-session token revocation via stored session ID (in database or cache)
• Optional caching layer to minimize DB hits
Final checklist
• Each component (Client, Express Server, Middlewares, Controllers, JWT Utils, Cache, Database, Socket.IO) is a distinct node.
• Labels for technologies (Express, Mongoose, Redis, Socket.IO).
• “single-session enforcement” near the JWT/utils or User model.