A RESTful microservice for managing polls built with Node.js, Express, and MongoDB.
- CRUD Operations: Create, read, update, and delete polls
- Search & Pagination: Search polls by question with pagination support
- Data Validation: Comprehensive input validation and sanitization
- Error Handling: Robust error handling with meaningful messages
- Native MongoDB: Uses MongoDB's native Node.js driver
- Node.js (v14 or higher)
- MongoDB (local installation or MongoDB Atlas account, can be changed in the .env)
- Clone the repository:
git clone https://github.com/PremPatel8/Polls.git
cd Polls- Install dependencies:
npm install- Create environment (.env) file for MongDB:
# Server Configuration
PORT=3000
# MongoDB Configuration
# MONGODB_URI=mongodb://localhost:27017
DB_NAME=pollsdb
# MongoDB Atlas example (you can switch between local MongoDB server and Atlas by commenting and uncommenting the right MONGODB_URI)
MONGODB_URI=mongodb+srv://username:[email protected]/pollsdb?retryWrites=true&w=majority&appName=Polls- Configure your environment variables in
.env:
PORT=3000
MONGODB_URI=mongodb://localhost:27017
DB_NAME=pollsdb
- Start the server:
npm start
# or for development with auto-reload
npm run devRetrieve all polls with optional pagination and search.
Query Parameters:
page(number): Page number (default: 1)limit(number): Items per page (default: 10, max: 100)search(string): Search in poll questionssortBy(string): Sort field (default: 'created_at')sortOrder(string): 'asc' or 'desc' (default: 'desc')
Example:
GET /polls?page=1&limit=5&search=color&sortBy=created_at&sortOrder=desc
Retrieve a single poll by ID.
Example:
GET /polls/65ecb4a9031983d68f635610
Create a new poll.
Request Body:
{
"question": "What is your favorite color?",
"options": ["Red", "Blue", "Green", "Yellow"]
}Update an existing poll.
Request Body:
{
"question": "What is your favorite programming language?",
"options": ["JavaScript", "Python", "Java", "Go"]
}Delete a poll by ID.
Example:
DELETE /polls/65ecb4a9031983d68f635610
- Question: Required, string, 1-500 characters
- Options: Required array, 2-10 items, each 1-100 characters, no duplicates
{
"message": "Poll created successfully",
"data": {
"_id": "65ecb4a9031983d68f635610",
"created_at": 1733687442,
"updated_at": 1733687442,
"question": "What is your favorite color?",
"options": ["Red", "Blue", "Green", "Yellow"]
}
}{
"error": "Validation error",
"message": "Invalid poll data",
"details": ["Question is required", "At least 2 options are required"]
}{
"data": [...],
"pagination": {
"currentPage": 1,
"totalPages": 5,
"totalCount": 50,
"limit": 10,
"hasNextPage": true,
"hasPrevPage": false,
"nextPage": 2,
"prevPage": null
},
"meta": {
"search": "color",
"sortBy": "created_at",
"sortOrder": "desc"
}
}Check service status:
GET /health