This is a simple Node.js Express application for a quiz app, providing an API to retrieve questions based on specified criteria.
This application serves as the backend for a quiz app, allowing users to retrieve questions based on their desired difficulty levels and total marks. The API is designed to be straightforward, with a single endpoint to request questions.
-
Clone the repository:
git clone https://github.com/alpha951/reelo
-
Navigate to the project directory:
cd reelo
-
Install dependencies:
npm install
-
Start the application:
node index.js
The application provides a RESTful API to fetch questions for a quiz. Refer to the API Endpoint and Request Body sections for details on making requests.
- Endpoint:
/v1/questions
- Method:
POST
The endpoint expects the following JSON structure in the request body:
{
"totalMarks": 100,
"easyPercentage": 30,
"mediumPercentage": 40,
"hardPercentage": 30
}
totalMarks
: Total marks for the quiz.easyPercentage
: Percentage of easy questions.mediumPercentage
: Percentage of medium questions.hardPercentage
: Percentage of hard questions.
The core functionality is implemented in the question.service.js
file. This service handles the loading and selection of questions based on the provided criteria.
Loads questions from the specified JSON file.
Selects questions based on the provided criteria (total marks and difficulty percentages).
req.body
{
"totalMarks": 100,
"easyPercentage": 20,
"mediumPercentage": 50,
"hardPercentage": 30
}
response
{
"success": true,
"message": "Successfully completed the request",
"data": {
"easy": [
{
"id": 1,
"question": "What is the speed of light",
"subject": "Physics",
"topic": "Motion",
"difficulty": "Easy",
"marks": 5
},
{
"id": 3,
"question": "If an object is moving with constant speed, what can you say about its acceleration?",
"subject": "Physics",
"topic": "Motion",
"difficulty": "Easy",
"marks": 5
},
{
"id": 6,
"question": "What is the molecular structure of water?",
"subject": "Chemistry",
"topic": "Chemical Bonding",
"difficulty": "Easy",
"marks": 5
},
{
"id": 10,
"question": "What are isotopes in the context of atomic structure?",
"subject": "Chemistry",
"topic": "Atomic Structure",
"difficulty": "Easy",
"marks": 5
}
],
"medium": [
{
"id": 2,
"question": "State the first law of thermodynamics.",
"subject": "Physics",
"topic": "Thermodynamics",
"difficulty": "Medium",
"marks": 10
},
{
"id": 4,
"question": "Solve the quadratic equation: (2x^2 - 5x + 2 = 0).",
"subject": "Mathematics",
"topic": "Quadratic Equations",
"difficulty": "Medium",
"marks": 10
},
{
"id": 7,
"question": "Balance the following chemical equation: (C_4H_{10} + O_2 rightarrow CO_2 + H_2O).",
"subject": "Chemistry",
"topic": "Chemical Equations",
"difficulty": "Medium",
"marks": 10
},
{
"id": 9,
"question": "Evaluate the integral: (int (3x^2 + 2x - 5) ,dx).",
"subject": "Mathematics",
"topic": "Integration",
"difficulty": "Medium",
"marks": 10
},
{
"id": 12,
"question": "Explain the concept of oxidation and reduction in chemical reactions.",
"subject": "Chemistry",
"topic": "Redox Reactions",
"difficulty": "Medium",
"marks": 10
}
],
"hard": [
{
"id": 5,
"question": "Define the concept of limit in calculus.",
"subject": "Mathematics",
"topic": "Calculus",
"difficulty": "Hard",
"marks": 15
},
{
"id": 8,
"question": "Explain the concept of wave-particle duality in quantum mechanics.",
"subject": "Physics",
"topic": "Quantum Mechanics",
"difficulty": "Hard",
"marks": 15
}
]
},
"error": {}
}
req.body
{
"totalMarks": 20,
"easyPercentage": 100,
"mediumPercentage": 0,
"hardPercentage": 0
}
response
{
"success": true,
"message": "Successfully completed the request",
"data": {
"easy": [
{
"id": 1,
"question": "What is the speed of light",
"subject": "Physics",
"topic": "Motion",
"difficulty": "Easy",
"marks": 5
},
{
"id": 3,
"question": "If an object is moving with constant speed, what can you say about its acceleration?",
"subject": "Physics",
"topic": "Motion",
"difficulty": "Easy",
"marks": 5
},
{
"id": 6,
"question": "What is the molecular structure of water?",
"subject": "Chemistry",
"topic": "Chemical Bonding",
"difficulty": "Easy",
"marks": 5
},
{
"id": 10,
"question": "What are isotopes in the context of atomic structure?",
"subject": "Chemistry",
"topic": "Atomic Structure",
"difficulty": "Easy",
"marks": 5
}
],
"medium": [],
"hard": []
},
"error": {}
}
The application uses the AppError
class for error handling. Specific error messages and HTTP status codes are provided for different scenarios, ensuring clear communication with the client.