This repository is a complete backend for a real-time polling application using:
- Node.js + Express
- PostgreSQL
- Prisma (ORM)
- Socket.IO (WebSocket)
- RESTful API for Users, Polls, Poll Options, and Votes.
- Prisma schema modeling one-to-many and many-to-many relationships.
- Real-time updates via Socket.IO rooms (one room per poll).
- Clone / unzip the project.
- Copy
.env.exampleto.envand updateDATABASE_URL. - Install dependencies:
npm install
- Generate Prisma client:
npx prisma generate
- Create your database (Postgres) and run migrations:
npx prisma migrate dev --name init
- Start the server:
npm run dev # or npm start - Server defaults to
http://localhost:4000.
POST /users— create user{ "name","email","password" }GET /users/:id— get user (no passwordHash returned)POST /polls— create poll with options:{ "question": "Your question?", "creatorId": 1, "options": ["a","b","c"], "isPublished": false }GET /polls— list pollsGET /polls/:id— get poll with options and vote countsPOST /polls/:pollId/vote— submit a vote{ "userId": 1, "optionId": 2 }PATCH /polls/:id/publish— publish a poll (sets isPublished = true)
- Clients should
joina poll room:socket.emit('join', { pollId }) - Server emits
poll:updateto that room when a new vote arrives with the updated option vote counts.
- Passwords are hashed with bcrypt.
- No authentication middleware included (left intentionally simple for screening). In a production system, add JWT/session-based auth, rate-limiting, validation, and input sanitization.
- Use SSL/TLS in production and secure your DATABASE_URL.
See prisma/schema.prisma for the data model.