https://github.com/jignesh6775/quiz_applicaton
This is the backend for a quiz application built using Node.js, Express.js, and MongoDB.
https://github.com/jignesh6775/quiz_applicaton
express-js mongodb mongoose nodejs quizapp rest-api
Last synced: 3 months ago
JSON representation
This is the backend for a quiz application built using Node.js, Express.js, and MongoDB.
- Host: GitHub
- URL: https://github.com/jignesh6775/quiz_applicaton
- Owner: Jignesh6775
- Created: 2023-12-19T11:03:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-19T15:03:35.000Z (over 2 years ago)
- Last Synced: 2025-07-19T08:02:46.007Z (12 months ago)
- Topics: express-js, mongodb, mongoose, nodejs, quizapp, rest-api
- Language: JavaScript
- Homepage: https://quiz-app-lp1g.onrender.com/
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Quiz Application Backend
This is the backend for a quiz application built using Node.js, Express.js, and MongoDB.
## Table of Contents
- [Installation](#installation)
- [Routes](#routes)
- [User Authentication](#user-authentication)
- [Quiz Management](#quiz-management)
- [Usage](#usage)
- [Creating a User Account](#creating-a-user-account)
- [Logging In](#logging-in)
- [Creating a Quiz](#creating-a-quiz)
- [Getting the Active Quiz](#getting-the-active-quiz)
- [Getting Quiz Results](#getting-quiz-results)
- [Getting All Quizzes](#getting-all-quizzes)
## Installation
1. Clone the repository:
```bash
git clone https://github.com/your-username/quiz-backend.git
cd quiz-backend
```
2. Install dependencies:
```bash
npm install
```
3. Set up your MongoDB connection by creating a `.env` file in the root directory with the following content:
```env
MONGO_URL=your-mongodb-connection-string
JWT_SECRET=your-secret-key
```
Replace `your-mongodb-connection-string` with your MongoDB connection string and `your-secret-key` with a secure secret key for JWT.
4. Run the application:
```bash
npm start
```
The server will be running on http://localhost:3000 by default.
## Routes
### User Authentication
| Method | Endpoint | Description | Authentication Required |
| ------ | ----------------- | ------------------------ | ------------------------ |
| POST | `/auth/signup` | Create a new user | No |
| POST | `/auth/login` | Log in an existing user | No |
### Quiz Management
| Method | Endpoint | Description | Authentication Required |
| ------ | --------------- | ----------------------------------- | ------------------------ |
| POST | `/quizzes` | Create a new quiz | Yes |
| GET | `/quizzes/active`| Get the currently active quiz | Yes |
| GET | `/quizzes/:id/result`| Get quiz results after 5 minutes of end time | Yes |
| GET | `/quizzes/all` | Get all quizzes (including inactive) | Yes |
## Usage
### Creating a User Account
**Endpoint**: `/auth/signup`
**Method**: POST
**Example Payload**:
```json
{
"username": "john_doe",
"password": "securepassword"
}
```
### Logging In
**Endpoint**: `/auth/login`
**Method**: POST
**Example Payload**:
```json
{
"username": "john_doe",
"password": "securepassword"
}
```
**Response**:
```json
{
"token": "your-json-web-token"
}
```
Use the obtained token for authentication in subsequent requests.
### Creating a Quiz
**Endpoint**: `/quizzes`
**Method**: POST
**Example Payload**:
```json
{
"title": "Sample Quiz",
"questions": [
{
"question": "What is the capital of France?",
"options": ["Berlin", "Paris", "Madrid", "Rome"],
"rightAnswer": 1
},
// ... (add more questions)
],
"startDate": "2023-12-15T08:00:00.000Z",
"endDate": "2023-12-15T10:00:00.000Z"
}
```
### Getting the Active Quiz
**Endpoint**: `/quizzes/active`
**Method**: GET
**Response**:
```json
{
"title": "Sample Quiz",
"questions": [
{
"question": "What is the capital of France?",
"options": ["Berlin", "Paris", "Madrid", "Rome"],
"rightAnswer": 1
},
// ... (more questions)
],
"startDate": "2023-12-15T08:00:00.000Z",
"endDate": "2023-12-15T10:00:00.000Z",
"active": true
}
```
### Getting Quiz Results
**Endpoint**: `/quizzes/:id/result`
**Method**: GET
**Example URL**: `/quizzes/your-quiz-id/result`
**Payload**: `Provide answers payload so it can compare your answers with right answer and return you result as response.`
```json
{
"answers":[1, 1, 0, 1, 1]
}
```
**Response**:
```json
{
"totalQuestions": 5,
"correctAnswers": 3,
"score": 60
}
```
### Getting All Quizzes
**Endpoint**: `/quizzes/all`
**Method**: GET
**Response**:
```json
[
{
"title": "Sample Quiz 1",
// ... (quiz details)
},
{
"title": "Sample Quiz 2",
// ... (quiz details)
},
// ... (more quizzes)
]
```