This project is a highly functional backend for a voting system, built using modern web technologies. It provides user and admin routes, allowing users to view candidates and cast their votes. The system ensures secure and efficient handling of voting processes, making it suitable for various voting scenarios.
- Node.js: JavaScript runtime for building the backend.
- Express.js: Web framework for Node.js.
- MongoDB: NoSQL database for storing user and voting data.
- JWT: JSON Web Tokens for secure authentication.
- Render: Cloud platform for hosting the backend, enabling seamless integration with frontend applications.
- User Registration and Login: Users can sign up and log in to the system.
- Candidate Display: Users can view a list of candidates available for voting.
- Voting: Authenticated users can cast their votes for their preferred candidates.
- Admin Management: Admins can add new candidates and view the voting results.
This backend can be hosted on Render for easy deployment and integration with frontend applications.
The Voting System Backend provides the following API endpoints:
- Endpoint:
POST /api/users/signup
- Description: Register a new user.
- Request Format:
{ "username": "string", "email": "string", "password": "string" }
- Response:
{ "message": "User registered successfully", "user": { "id": "string", "username": "string", "email": "string" } }
- Endpoint:
POST /api/users/login
- Description: Authenticate a user.
- Request Format:
{ "email": "string", "password": "string" }
- Response:
{ "message": "User logged in successfully", "token": "string" }
- Endpoint:
GET /api/users/candidates
- Description: Retrieve a list of candidates available for voting.
- Response:
{ "candidates": [ { "id": "string", "name": "string", "description": "string" }, ... ] }
- Endpoint:
POST /api/users/vote
- Description: Cast a vote for a candidate.
- Request Format:
{ "candidateId": "string" }
- Response:
{ "message": "Vote cast successfully" }
- Endpoint:
POST /api/admin/candidates
- Description: Add a new candidate.
- Request Format:
{ "name": "string", "description": "string" }
- Response:
{ "message": "Candidate added successfully", "candidate": { "id": "string", "name": "string", "description": "string" } }
- Endpoint:
GET /api/admin/votes
- Description: Retrieve a list of votes cast.
- Response:
{ "votes": [ { "id": "string", "userId": "string", "candidateId": "string", "timestamp": "string" }, ... ] }
-
Sign Up:
- Send a
POST
request to/api/users/signup
with the following JSON body:{ "username": "john_doe", "email": "john@example.com", "password": "password123" }
- You will receive a response confirming the registration.
- Send a
-
Login:
- Send a
POST
request to/api/users/login
with the following JSON body:{ "email": "john@example.com", "password": "password123" }
- You will receive a token in the response, which you will use for authenticated requests.
- Send a
-
View Candidates:
- Send a
GET
request to/api/users/candidates
. - You will receive a list of candidates available for voting.
- Send a
-
Vote for a Candidate:
- Send a
POST
request to/api/users/vote
with the following JSON body:{ "candidateId": "candidate_id_here" }
- You will receive a response confirming that your vote has been cast.
- Send a
-
Add a Candidate:
- Send a
POST
request to/api/admin/candidates
with the following JSON body:{ "name": "Jane Doe", "description": "A brief description of the candidate." }
- You will receive a response confirming that the candidate has been added.
- Send a
-
View Votes:
- Send a
GET
request to/api/admin/votes
. - You will receive a list of votes cast in the system.
- Send a
To get started with the Voting System Backend, follow these steps:
-
Clone the repository:
git clone /~https://github.com/yourusername/voting-system-backend.git cd voting-system-backend
-
Install dependencies:
npm install
-
Set up environment variables: Create a .env file in the root directory and add the following variables:
DATABASE_URL=your_database_url JWT_SECRET=your_jwt_secret
-
Run the application:
npm start
The backend server should now be running on http://localhost:3000
.