How shivanshusonwani/codecanvas works
1. Overall shape
- CodeCanvas is a two-tier application: a React/Vite frontend in client/ and an Express-based API in server/.
- The main product flow is an authenticated code-editor workspace with live preview and persistence for saved canvas content.
2. Frontend shell
- The React app is assembled in client/src/main.jsx and App.jsx, with page-level routing/sectioning across Home, Login, Signup, and Canvas.
- Navbar and the CSS files provide the shared layout and visual structure for the app.
3. Editor workspace
- The core interactive area is the Canvas page, which combines the code editor and preview experience.
- MonacoEditor.jsx is the editing surface for HTML/CSS/JS, while CanvasPreview.jsx is responsible for rendering the output in a sandboxed preview surface, consistent with the README’s iframe-based live preview model.
4. Client-side state and auth
- AuthContext.jsx centralizes authentication state for the frontend.
- This context is the boundary between login/signup flows and protected app areas, so the UI can react to signed-in state without threading auth props through every component.
5. Client-to-server API boundary
- client/src/api.js is the frontend’s API abstraction layer.
- It isolates HTTP calls from the UI components/pages, making auth and canvas persistence requests go through a single client-side integration point.
6. Backend entry and app composition
- server/server.js is the process entrypoint, and server/src/app.js composes the Express application.
- This separation suggests a clean bootstrapping boundary: startup/environment setup in one place, middleware/routes/configuration in another.
7. Backend domains
- The server is split by concern into auth and canvas domains, each with its own controller and route files.
- auth.controller.js and auth.routes.js handle user registration/login-related flows, while canvas.controller.js and canvas.routes.js handle editor document persistence and retrieval.
8. Data and persistence
- user.model.js and canvas.model.js represent the two persistent entities in the system: users and saved canvas content.
- config/db.js establishes the database connection, so persistence is a core part of the architecture rather than an afterthought.
9. Protected backend access
- auth.middleware.js is the security boundary for server endpoints that require an authenticated user.
- Canvas-related actions are likely gated here so only authorized requests can read/write persisted canvases.
10. Runtime and tooling
- The frontend uses React with Vite, and the backend uses a Node.js/Express stack.
- The architecture is designed for fast local development and interactive editing, with the editor runtime, preview runtime, and persistence layer kept deliberately separated.
11. Key data flow to diagram
- User signs in or signs up through the frontend, which calls the auth API and stores auth state in AuthContext.
- In the Canvas workspace, editor changes update the live preview immediately on the client, and saved work can be sent to the backend canvas API for persistence.
- The backend validates/authenticates requests, reads/writes via the models, and returns data back to the UI.
12. Important boundaries
- Keep the preview sandbox separate from the editor state: the preview is a render target, not the source of truth.
- Keep auth state on the client distinct from backend authorization checks: AuthContext manages UX state, while middleware enforces access control on the server.
Main entry points: App.jsx Routes · Canvas.jsx Canvas page · api.js API client · canvas.routes.js Canvas routes
Components 21
Client
React/Vite frontend workspace and editor UI.
- App shell · Bootstraps the frontend React tree.
client/src/main.jsx - Routes · Assembles the top-level app structure and page sections.
client/src/App.jsx - Navbar · Shared navigation and header chrome.
client/src/components/Navbar.jsx - Home page · Public entry page for the product.
client/src/pages/Home.jsx - Login page · User sign-in surface.
client/src/pages/Login.jsx - Signup page · User registration surface.
client/src/pages/Signup.jsx - Canvas page · Authenticated editor workspace that combines editing, preview, and save flows.
client/src/pages/Canvas.jsx - Editor · Monaco-based HTML/CSS/JS editing surface.
client/src/components/MonacoEditor.jsx - Preview · Sandboxed live preview of the canvas output.
client/src/components/CanvasPreview.jsx - Auth state · Frontend authentication state and session boundary.
client/src/context/AuthContext.jsx - API client · Single client-side integration point for backend requests.
client/src/api.js
Server
Express API, domain logic, and persistence.
- Server boot · Process entrypoint and startup wiring.
server/server.js - Express app · Composes middleware, routing, and server configuration.
server/src/app.js - Auth routes · Registration and login endpoints.
server/src/routes/auth.routes.js - Canvas routes · Persistence endpoints for saved canvas content.
server/src/routes/canvas.routes.js - Auth logic · Handles signup/login request behavior.
server/src/controllers/auth.controller.js - Canvas logic · Handles canvas save/read behavior.
server/src/controllers/canvas.controller.js - Auth guard · Enforces authenticated access on protected endpoints.
server/src/middleware/auth.middleware.js - Users · Persistent user entity.
server/src/models/user.model.js - Canvases · Persistent canvas document entity.
server/src/models/canvas.model.js - Database · Database connection setup used by the models.
server/src/config/db.js
Connections 26
- App shell → Routes: mounts · Frontend entry renders the app shell.
- Routes → Navbar: shared chrome · App shell includes common navigation.
- Routes → Home page: routes · Public landing section.
- Routes → Login page: routes · Sign-in section.
- Routes → Signup page: routes · Registration section.
- Routes → Canvas page: routes · Authenticated editor workspace.
- Canvas page → Editor: edits · Canvas page hosts the code editor.
- Canvas page → Preview: renders · Canvas page pushes output to the preview surface.
- Canvas page → Auth state: checks auth · Canvas behavior depends on client auth state.
- Login page → Auth state: updates · Login flow updates frontend auth state.
- Signup page → Auth state: updates · Signup flow updates frontend auth state.
- Login page → API client: calls · Auth requests go through the API layer.
- Signup page → API client: calls · Registration requests go through the API layer.
- Canvas page → API client: saves · Workspace persistence uses the API client.
- API client → Auth routes: auth api · Frontend auth requests reach server auth endpoints.
- API client → Canvas routes: canvas api · Frontend canvas requests reach server canvas endpoints.
- Server boot → Express app: starts · Entry script boots the Express app.
- Express app → Auth routes: mounts · Express app exposes auth routes.
- Express app → Canvas routes: mounts · Express app exposes canvas routes.
- Auth routes → Auth logic: delegates · Auth routing passes to controller logic.
- Canvas routes → Canvas logic: delegates · Canvas routing passes to controller logic.
- Canvas routes → Auth guard: protects · Canvas access is gated by auth middleware.
- Auth logic → Users: reads/writes · Auth logic persists and looks up users.
- Canvas logic → Canvases: reads/writes · Canvas logic persists and retrieves canvases.
- Users → Database: uses · User model depends on the DB connection.
- Canvases → Database: uses · Canvas model depends on the DB connection.