The Polling API Backend is a Node.js application that serves as the backend for a polling system. It provides endpoints for creating questions, managing options, user authentication using JSON Web Tokens (JWT), and role-based access control (RBAC) for certain actions.
- Features
- Technologies Used
- Installation
- Usage
- API Endpoints
- Authentication
- Authorization
- Contributing
- License
- Question Management: Create, view, and retrieve questions.
- Option Management: Add options to questions and delete options.
- User Authentication: Register, login, and receive JWT tokens for authentication.
- Role-Based Access Control (RBAC): Differentiate between administrators and regular users for specific actions.
- Node.js
- Express.js
- JavaScript
- MongoDB
- JSON Web Tokens (JWT) for authentication
-
Clone the repository:
git clone https://github.com/deepak5204/PollingAPIRevamp.git
-
Navigate to the project directory:
cd pollingAPIRevamp
-
Install dependencies:
npm install
-
Set up environment variables. Create a .env file in the root directory and add the following:
- PORT=5000 - MONGODB_URI=your_mongodb_connection_string - JWT_SECRET=your_jwt_secret_key - JWT_EXPIRESIN = 5h
Replace your_mongodb_connection_string with your MongoDB connection string and your_jwt_secret_key with a secret key for JWT token generation.
- Start the server:
npm start
To use the API, you can interact with it using tools like Postman, cURL, or any other HTTP client.
-
Only administrators can create, update, and delete questions and options.
-
Regular users have view-only access to questions and options.
-
POST /v2/user/signup: Register a new user.
curl -X POST http://localhost:5000/v2/user/signup \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "exampleuser",
"email": "[email protected]",
"phoneNo": "123456789",
"password": "examplepassword"
}'
This command sends a POST request to the '/v2/user/signup' endpoint with a JSON payload containing the user's name, email, phone number, and password. It registers a new user with the provided information.
- POST /v2/user/login: Login and get a JWT token.
curl -X POST http://localhost:5000/v2/user/login \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "[email protected]",
"password": "examplepassword"
}'
This command sends a POST request to the '/v2/user/login' endpoint with a JSON payload containing the user's email and password. It authenticates the user and returns a JWT token if the credentials are valid.
Make sure to replace http://localhost:5000 with the actual base URL of your API server.
- POST /v2/question : Create a new question.
- GET /v2/question/:questionId : Get a specific question by ID.
- GET /v2/question : Get all questions.
- DELETE /v2/question/:questionId : Delete a specific question by ID.
- POST /v2/option/:questionId : Add an option to a question.
- GET /v2/option/addvote/:optionId : Add vote an option for a question.
- DELETE /v2/option/:optionId : Delete an option from a question.