How the-marcy-lab-school/react-express-auth works
Project Type and Purpose
• This is a full-stack web application that provides user authentication and profile management.
• Frontend: React SPA (Vite)
• Backend: Node.js + Express with cookie-based sessions and a PostgreSQL database accessed via Knex.js
Main Architectural Components
a. Client / Browser
• Runs the React single‐page application (SPA).
• Hosts UI components, pages, contexts, and adapters for communicating with the backend.
b. Frontend Hosting (Static Assets)
• Built by npm run build (Vite) into a static bundle (HTML/JS/CSS).
• Can be served via a CDN, nginx, or the Express server itself.
c. Backend API Server (Express)
• Exposes RESTful endpoints under /auth and /users.
• Middleware stack: request logging → cookie session handling → authentication check → route handlers → error logging/handling.
• Controllers (authControllers.js, userControllers.js) implement business logic.
d. Database Layer
• PostgreSQL database defined by .env configuration.
• Knex.js for migrations, seeds, and query building.
• Data model: User in server/models/User.js
e. External & Dev Tools
• Postgres (external service)
• Knex migrations & seeds
• Environment variables (.env)
Relationships & Data Flow
1. User opens browser → React app is loaded (index.html + bundle).
2. React components → call fetch() utility in src/utils → adapters (auth-adapter, user-adapter) → HTTP requests to Express API.
3. Express middleware parses cookies, establishes req.session.userId, and logs the request.
4. Controllers call model methods → Knex queries → Postgres database.
5. Responses (JSON) flow back through middleware → to React adapters → update context & components.
6. Session cookie is set/updated in browser on login/logout.
Architectural Patterns & Design Principles
• Separation of Concerns:
• Frontend: UI / state management / API adapters
• Backend: HTTP layer / middleware / controllers / models
• Layered Architecture on Server:
1. Middleware layer (authentication, sessions, logging, error handling)
2. Controller layer (HTTP → business logic)
3. Data access layer (Knex + Models → DB)
• Component-Based UI on Frontend:
• Pages, presentational components, context providers, utility modules
Final Checklist
• Grouping or layering to show middleware → controllers → models on the server
• Annotation of key technologies: React, Vite, Express, Knex, PostgreSQL