# vkantb05/voting-app-backend-ejs architecture

> Architecture diagram of the GitHub repository vkantb05/voting-app-backend-ejs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vkantb05/voting-app-backend-ejs
- Repository: https://github.com/vkantb05/voting-app-backend-ejs

## Overview

1. **High-level shape**
   This is a server-rendered Node.js voting application, not a client-side SPA. The main architecture is an Express app that renders EJS pages, persists data in MongoDB, and uses auth/session logic to separate voters from admins.

2. **Entry point and app composition**
   `server.js` is the runtime entry point. It likely wires together Express middleware, connects to the database via `db.js`, mounts the route modules, serves static assets from `public/`, and starts listening on the configured port.

3. **Persistence boundary**
   MongoDB is the only visible data store. The `models/` folder contains two data models: `user.js` and `candidate.js`. That strongly suggests two core collections:
   - users for accounts, roles, and voting state
   - candidates for election entries and vote counts or related metadata

4. **Authentication and authorization**
   `jwt.js` plus the README’s mention of sessions and bcrypt indicate the app has an auth layer for login/signup and protected routes. Access is role-based: admins can manage candidates, while voters can browse and cast a vote. The architecture likely enforces this at the route level rather than in a separate service.

5. **Route boundaries**
   The application is split into two main route groups:
   - `routes/UserRoute.js` for signup, login, profile editing, and user-facing account flows
   - `routes/CandidateRoute.js` for candidate CRUD, voting actions, and results
   These route modules act as the main application boundary between HTTP requests and model operations.

6. **Server-side presentation**
   The `views/` directory contains EJS templates for all primary screens: login, signup, profile, edit profile, candidate management, voting, results, and confirmation states like `voted`. This indicates the UI is generated on the server and the browser receives HTML pages, not an API-first frontend.

7. **Static assets**
   `public/stylesheet/style.css` is the only obvious frontend asset pipeline. Styling is handled by static CSS served directly by Express. There is no evidence of a build step, frontend framework, or component bundler.

8. **Core data flows**
   The main request flows are:
   - user registers or logs in
   - server authenticates the user and establishes session/auth state
   - voter browses candidates and submits one vote
   - admin creates/edits/deletes candidates and checks results
   - app reads/writes MongoDB through the models and renders the next EJS view

9. **Voting rule enforcement**
   A key business boundary is the “one vote per user” rule described in the README. That logic likely lives in the candidate/user route handling and user model state, making it an application-level constraint rather than a database-only rule.

10. **Operational/runtime concerns**
   The README shows environment-driven deployment with `PORT`, `MONGO_URI`, and `SESSION_SECRET`. That implies the app is built to run the same way locally and in cloud deployment environments such as Render or Railway, with configuration injected at runtime.

11. **What is notably absent**
   There is no evidence of separate controllers, services, background jobs, queues, or real-time sockets. The architecture appears intentionally compact: routes + models + views + static assets, with authentication and persistence handled inside the same Node/Express process.

12. **How to draw the diagram**
   A good architecture diagram would show:
   - browser/client on the left
   - Express app in the middle
   - route modules branching into user and candidate flows
   - EJS rendering back to the browser
   - MongoDB on the data side through `db.js` and the models
   - `public/` as a static asset path served directly by the app
   - auth/session/JWT logic as a cross-cutting concern around protected routes

Main entry points: [`server.js`](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/server.js) (App entry), [`routes/UserRoute.js`](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/routes/UserRoute.js) (User routes), [`routes/CandidateRoute.js`](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/routes/CandidateRoute.js) (Candidate routes), [`jwt.js`](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/jwt.js) (Auth layer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_client["Client"]
  node_browser(("Browser<br/>client"))
end

subgraph group_g_runtime["App runtime"]
  node_server_js["App entry<br/>express bootstrap<br/>[server.js]"]
  node_db_js["DB connect<br/>[db.js]"]
  node_jwt_js["Auth layer<br/>session/jwt auth<br/>[jwt.js]"]
end

subgraph group_g_http["HTTP routes"]
  node_user_routes["User routes<br/>http routes<br/>[UserRoute.js]"]
  node_candidate_routes["Candidate routes<br/>http routes<br/>[CandidateRoute.js]"]
end

subgraph group_g_data["Data layer"]
  node_user_model[("User model<br/>mongo model<br/>[user.js]")]
  node_candidate_model[("Candidate model<br/>mongo model<br/>[candidate.js]")]
  node_mongo[("MongoDB<br/>database")]
end

subgraph group_g_ui["Server views"]
  node_view_login["Login page<br/>ejs view<br/>[login.ejs]"]
  node_view_signup["Signup page<br/>ejs view<br/>[signup.ejs]"]
  node_view_profile["Profile page<br/>ejs view<br/>[profile.ejs]"]
  node_view_candidates["Candidates page<br/>ejs view<br/>[candidate.ejs]"]
  node_view_results["Results page<br/>ejs view<br/>[result.ejs]"]
  node_view_voted["Voted page<br/>ejs view<br/>[voted.ejs]"]
end

subgraph group_g_assets["Static assets"]
  node_style_css["Stylesheet<br/>static css<br/>[style.css]"]
end

node_browser -->|"HTTP requests"| node_server_js
node_server_js -->|"connects"| node_db_js
node_server_js -->|"uses auth"| node_jwt_js
node_server_js -->|"mounts"| node_user_routes
node_server_js -->|"mounts"| node_candidate_routes
node_server_js -->|"serves"| node_style_css
node_user_routes -->|"protects"| node_jwt_js
node_candidate_routes -->|"protects"| node_jwt_js
node_user_routes -->|"reads/writes"| node_user_model
node_candidate_routes -->|"reads/writes"| node_candidate_model
node_user_model -->|"persists"| node_mongo
node_candidate_model -->|"persists"| node_mongo
node_user_routes -->|"renders"| node_view_login
node_user_routes -->|"renders"| node_view_signup
node_user_routes -->|"renders"| node_view_profile
node_candidate_routes -->|"renders"| node_view_candidates
node_candidate_routes -->|"renders"| node_view_results
node_candidate_routes -->|"renders"| node_view_voted
node_server_js -->|"HTML response"| node_browser

click node_server_js "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/server.js"
click node_db_js "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/db.js"
click node_jwt_js "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/jwt.js"
click node_user_routes "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/routes/UserRoute.js"
click node_candidate_routes "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/routes/CandidateRoute.js"
click node_user_model "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/models/user.js"
click node_candidate_model "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/models/candidate.js"
click node_view_login "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/login.ejs"
click node_view_signup "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/signup.ejs"
click node_view_profile "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/profile.ejs"
click node_view_candidates "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/candidate.ejs"
click node_view_results "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/result.ejs"
click node_view_voted "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/voted.ejs"
click node_style_css "https://github.com/vkantb05/voting-app-backend-ejs/blob/main/public/stylesheet/style.css"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser toneBlue
class node_server_js,node_db_js,node_jwt_js toneAmber
class node_user_routes,node_candidate_routes toneMint
class node_user_model,node_candidate_model,node_mongo toneRose
class node_view_login,node_view_signup,node_view_profile,node_view_candidates,node_view_results,node_view_voted toneIndigo
class node_style_css toneTeal
```

## Components

### Client

Browser-side request and page consumption

- **Browser**: client · User agent that receives rendered HTML and submits forms

### App runtime

Express entry point and cross-cutting middleware

- [App entry](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/server.js): express bootstrap · Wires middleware, routes, static assets, and server startup · `server.js`
- [DB connect](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/db.js): mongodb connector · Creates and manages the MongoDB connection · `db.js`
- [Auth layer](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/jwt.js): session/jwt auth · Handles login state and route protection · `jwt.js`

### HTTP routes

Request handlers for account and voting flows

- [User routes](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/routes/UserRoute.js): http routes · Signup, login, profile, and account flows · `routes/UserRoute.js`
- [Candidate routes](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/routes/CandidateRoute.js): http routes · Candidate management, voting, and results flows · `routes/CandidateRoute.js`

### Data layer

MongoDB connection and collections

- [User model](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/models/user.js): mongo model · Accounts, roles, and vote-state records · `models/user.js`
- [Candidate model](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/models/candidate.js): mongo model · Candidates and vote-related records · `models/candidate.js`
- **MongoDB**: database · Primary persistent data store

### Server views

EJS pages rendered by the app

- [Login page](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/login.ejs): ejs view · Sign-in screen rendered by the server · `views/login.ejs`
- [Signup page](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/signup.ejs): ejs view · Account creation screen rendered by the server · `views/signup.ejs`
- [Profile page](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/profile.ejs): ejs view · User account summary and profile screen · `views/profile.ejs`
- [Candidates page](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/candidate.ejs): ejs view · Candidate listing and voting interface · `views/candidate.ejs`
- [Results page](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/result.ejs): ejs view · Vote totals and election outcome screen · `views/result.ejs`
- [Voted page](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/views/voted.ejs): ejs view · Confirmation shown after a successful vote · `views/voted.ejs`

### Static assets

Directly served CSS

- [Stylesheet](https://github.com/vkantb05/voting-app-backend-ejs/blob/main/public/stylesheet/style.css): static css · Global presentation styling served directly · `public/stylesheet/style.css`

## Connections

- Browser → App entry: HTTP requests · Browser submits form posts and page requests to the Express app
- App entry → DB connect: connects · Bootstraps the database connection during startup
- App entry → Auth layer: uses auth · Applies auth/session handling across protected routes
- App entry → User routes: mounts · Registers the user-facing route module
- App entry → Candidate routes: mounts · Registers the candidate and voting route module
- App entry → Stylesheet: serves · Exposes static CSS assets to the browser
- User routes → Auth layer: protects · Uses auth checks for login and profile flow
- Candidate routes → Auth layer: protects · Uses auth checks for voting and admin actions
- User routes → User model: reads/writes · Creates accounts and updates user state
- Candidate routes → Candidate model: reads/writes · Manages candidates and vote data
- User model → MongoDB: persists · Stores user documents in MongoDB
- Candidate model → MongoDB: persists · Stores candidate documents in MongoDB
- User routes → Login page: renders · Returns the login page
- User routes → Signup page: renders · Returns the signup page
- User routes → Profile page: renders · Returns the profile page
- Candidate routes → Candidates page: renders · Returns the candidate browsing and voting page
- Candidate routes → Results page: renders · Returns election results
- Candidate routes → Voted page: renders · Returns vote confirmation after submission
- App entry → Browser: HTML response · Sends rendered pages back to the client
