A RESTful API backend for a voting system built with Express.js and MongoDB.
- Node.js (v14 or higher)
- MongoDB (running locally on port 27017)
- Clone the repository:
git clone https://github.com/yourusername/voting-system.git
cd voting-system/voting-system-backend
- Install dependencies:
npm install
- Set up environment variables:
Create a
.env
file in the root directory and add the following variables:
SECRET_KEY=your_jwt_secret_key_here
- Make sure MongoDB is running locally
- Start the application:
node .
The server will start on port 3000 by default.
- POST /api/v1/auth/register - Register a new user
- POST /api/v1/auth/login - Login with username and password
- GET /api/v1/users/me - Get current user info
- GET /api/v1/users - Get all users (admin only)
- DELETE /api/v1/users?id=userId - Delete a user (admin only)
- GET /api/v1/votings - Get all voting options (admin only)
- POST /api/v1/votings - Create a new voting option
- GET /api/v1/votings/vote?id=votingId - Vote for an option
- DELETE /api/v1/votings?id=votingId - Delete a voting option (admin only)
The application uses Jest and Supertest for testing.
- Make sure MongoDB is running locally
- Run the tests:
npm test
This will start Jest in watch mode, which will automatically rerun tests when files change.
-
src/ - Source code
- controllers/ - Route handlers
- middlewares/ - Express middlewares
- models/ - Mongoose models
- response/ - Response formatting
- services/ - Business logic
- app.js - Express application setup
- index.js - Application entry point
-
tests/ - Test files
- The admin user is automatically created when registering with the username "admin"
- Users can only vote once
- Deleted users and voting options are soft-deleted (marked as deleted in the database)