How nguyendatgh/chat_app-serverside works
Main Components
• Express Server: The backbone of the application handling HTTP requests, middleware execution, and routing.
• Real-Time Socket Server: A dedicated module (under src/controllers/socket.ts) that manages web socket connections for real-time chat functionality.
• API Controllers & Routes: Present in the src/controllers and src/routes directories, these manage endpoints for authentication, contact management, conversations, and user operations.
• Middlewares: Modules like verifyToken and validate (in src/middlewares) that enforce security, token validation, and input validation.
• Prisma & PostgreSQL: The data layer that persists user, contact, and chat data. This component shows the use of Prisma for schema management, migrations, and generating the client.
• Utility and Validation Layers: Additional helper functions (in src/utils, src/validations, and src/validators) that ensure data consistency and might perform business logic checks.
Relationships and Interactions
• The client (front-end, although not part of this codebase, is implied) sends HTTP requests to the Express server (API endpoints).
• HTTP requests pass through middlewares that perform validation and authentication checks (e.g., verifyToken) before reaching the controllers.
• Controllers use services (which may internally use utilities, validators, and Prisma Client) to interact with the PostgreSQL database, executing CRUD operations.
• For real-time communication, the socket controller manages persistent socket connections where data messages flow via socket.io channels.
Architectural Patterns and Design Principles
• MVC (or similar layered pattern): Routes delegate requests to controllers, which then use middlewares and validations; separate concerns are maintained between handling requests, business logic, and data persistence.
• Real-time Communication: Use of socket.io indicates an event-driven model to support dynamic message exchange.
• Security and Data Integrity: JWT-based authentication and token verification middleware ensure that only authenticated requests proceed into the system.
Technologies, Frameworks, and Libraries
• Node.js with Express for HTTP server operations.
• TypeScript for type safety and structured code.
• Prisma as the ORM interfacing with a PostgreSQL database.
• Socket.io for managing real-time web socket connections.
• Libraries such as bcrypt, cors, dotenv, jsonwebtoken, and yup to handle security, environment configuration, and validation.
Specific Elements
• Labels for each major module/component (Express Server, Socket Server, Controllers, Middlewares, Prisma Client, PostgreSQL Database, etc.).
• From client requests to Express routes.
• From routes through middlewares to controllers.
• From controllers to the database via Prisma.
• Between the socket module and connected clients for real-time event handling.
• Notes indicating where security (JWT authentication) and validation (yup) are enforced.