demo-small.mp4
A real-time voting API built with Node.js, Express, PostgreSQL, Prisma, and Socket.IO.
Supports authentication, poll creation, voting, and live updates.
- 🔑 User registration & login with JWT authentication
- 📊 Create and list polls
- ✅ Cast votes (1 per user per poll)
- 🔄 Real-time updates via WebSockets
- ⚡ Centralized validation & error handling
- 🧪 Integration tests (Jest + Supertest)
- 🐳 Docker setup for easy run
- 📬 Postman collection & environment included
- ⚙️ Node.js (>= 18) and npm
- 🐘 PostgreSQL or Docker
- 📮 Postman (for API testing)
## 🛠️ Setup & Run (Local)
```bash
1️⃣ Clone and install
git clone https://github.com/konvictgit/Move37-Voting-API
cd move37-voting
npm install
2️⃣ Configure environment
A .env file is already included. Example:
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/voting"
JWT_SECRET="change-me"
3️⃣ Setup database
npx prisma generate
npx prisma migrate dev --name init
node prisma/seed.js
4️⃣ Start server
npm run dev
Server runs at 👉 http://localhost:4000
🧪 Quick API Test (curl)
🔍 Health check:
curl http://localhost:4000/
👤 Register user:
curl -X POST http://localhost:4000/users \
-H "Content-Type: application/json" \
-d '{"name":"Bob","email":"bob@example.com","password":"secret"}'
🔑 Login:
curl -X POST http://localhost:4000/users/login \
-H "Content-Type: application/json" \
-d '{"email":"alice@example.com","password":"password"}'
📊 Create Poll:
curl -X POST http://localhost:4000/polls \
-H "Authorization: Bearer <TOKEN>" \
-H "Content-Type: application/json" \
-d '{"question":"Best season?","options":["Winter","Summer"]}'
🗳️ Vote:
curl -X POST http://localhost:4000/polls/<POLL_ID>/vote \
-H "Authorization: Bearer <TOKEN>" \
-H "Content-Type: application/json" \
-d '{"optionId":"<OPTION_ID>"}'
📈 Poll results:
curl http://localhost:4000/polls/<POLL_ID>
🌐 WebSocket Tests
🔌 A. Node listener (listen.js)
node listen.js <POLL_ID>
Then cast a vote → see pollUpdated.
🌍 B. Browser listener (ws-test.html)
Set pollId in script.
Open in browser.
Cast a vote → live updates.
🎭 C. Full demo (poll-demo.html)
Set:
const pollId = "your-poll-id";
const token = "your-jwt-token";
Start a static server:
npx http-server .
Open http://localhost:8080/poll-demo.html
.
Open in two tabs → vote → both update live.
📬 Postman Setup
📂 Files:
Move37-Voting.postman_collection.json
👉 Flow:
Import into Postman.
Run Register User (optional).
Run Login User → saves token automatically.
Run Create Poll
Run Vote
Run Get Poll by ID → see votes.
🧪 Run Tests
npm test
Covers:
👤 User registration & login
📊 Poll creation & listing
🗳️ Voting flow & duplicate prevention
⚠️ Validation errors
🐳 Run with Docker
docker-compose up --build
API → http://localhost:4000
DB → localhost:5432
Seed DB if needed:
docker exec -it <app_container_name> node prisma/seed.js
👩💻 Seeded Users
👩 Alice — alice@example.com / password
👨 Bob — bob@example.com / password
👦 Charlie — charlie@example.com / password