How prince-1501/voting_app works
Project Type and Purpose
• This is a RESTful backend service for a voting system built with Node.js/Express, MongoDB for persistence, and JWT for stateless authentication. There is no bundled frontend—clients (web or mobile) will consume its API.
File Structure
• Top-level
• server.js – bootstraps the Express app and wires up middleware, routes, database, and JWT.
• db.js – handles MongoDB connection logic.
• jwt.js – configures JWT signing and verification.
• routes/ – Express route definitions for user and candidate endpoints.
• models/ – Mongoose models for User and Candidate entities.
Extract Architecture Details from README
• Stateless REST API with endpoints for signup/login, candidate CRUD, voting, and profile management.
• Access control: Admin vs Regular User enforced via JWT claims.
• Persistence: MongoDB collections for user and candidate data.
Main Components
a. Client Layer
• Web/Mobile Client (Browser or App)
b. API Layer (Express Server)
• Authentication Middleware (JWT)
• Route Handlers (UserRoutes, CandidateRoutes)
• Business Logic / Controllers (embedded in route handlers)
c. Data Layer
• MongoDB Database
• Mongoose Models (User, Candidate)
d. (Optional) Test Data Injection
• Dummy Data Scripts
e. External Services (if any in future, e.g., Email/SMS for notifications)
Relationships & Data Flows
1. Client → POST /signup or /login → Auth Middleware → Server Issues JWT → Client
2. Client → GET/POST/PUT/DELETE /candidates (with JWT) → Route Handler → MongoDB → Response
3. Client → POST /candidates/vote/:id → Business Logic enforces “one vote per user” → Update Candidate model → Response
4. Client → GET /users/profile → Auth → Model → Response
• Token flow: after login, Client includes “Authorization: Bearer <JWT>” header on subsequent calls.
Architectural Patterns & Principles
• MVC-like separation:
• Models → Mongoose schemas
• Controllers/Routes → Express handlers
• Middleware → JWT auth layer
• Statelessness: no session storage on server
• Role-based access control (RBAC) scoped in middleware
Technologies & Frameworks
• Node.js and Express.js (API Server)
• MongoDB (NoSQL database)
• Mongoose (ODM library)
• JSON Web Tokens (jwt.js) for authentication
• Optional: dotenv for environment variables (not shown but likely present)