This API provides endpoints to manage questions and answers, allowing users to create, retrieve, update, and delete questions, as well as interact with answers by upvoting or downvoting.
This backend project is built using Node.js and Express for robust server-side JavaScript development. Here's why these technologies were chosen:
- Node.js: Chosen for its asynchronous and event-driven architecture, ideal for handling multiple concurrent requests efficiently.
- Express: Selected for its minimalistic yet powerful framework that simplifies routing, middleware management, and handling HTTP requests.
- PostgreSQL: Used as the relational database to store questions, answers, and user data, ensuring data integrity and scalability.
- Rate Limiting Middleware: Custom rate limiting middleware is implemented to mitigate abuse and ensure fair usage of the API.
- JWT Authorization: Implemented for securing endpoints, ensuring that only authenticated users can access certain routes.
- Swagger: Integrated for API documentation, providing clear and interactive documentation for developers consuming the API.
-
GET
/questions
Retrieve a list of all questions.
Parameters:
title
: (optional) Title to filter questionscategory
: (optional) Category to filter questions
Responses:
200 OK
: A list of questions404 Not Found
: Questions not found500 Internal Server Error
: Server error
-
POST
/questions
Create a new question.
Request Body:
{
"title": "Example Question",
"description": "This is an example question.",
"category": "Example Category"
}
Responses:
-
POST
/questions/{id}/answers
Create an answer for a specific question identified by ID.
Path Parameter:
id
: ID of the question to which the answer belongs
Request Body:
{
"content": "This is an example answer."
}
Responses:
201 Created
: Answer created successfully400 Bad Request
: Missing or invalid request data404 Not Found
: Question not found500 Internal Server Error
: Server error
-
POST
/questions/{id}/upvote
Upvote a specific question identified by ID.
Path Parameter:
id
: ID of the question to upvote
Responses:
-
200 OK
: Successfully upvoted the question -
404 Not Found
: Question not found -
500 Internal Server Error
: Server error
-
POST
/questions/{id}/downvote
Downvote a specific question identified by ID.
Path Parameter:
id
: ID of the question to downvote
Responses:
-
200 OK
: Successfully downvoted the question -
404 Not Found
: Question not found -
500 Internal Server Error
: Server error
-
GET
/questions/{id}
Retrieve a question by its ID.
Path Parameter:
id
: ID of the question to retrieve
Responses:
-
200 OK
: A single question -
404 Not Found
: Question not found -
500 Internal Server Error
: Server error
-
PUT
/questions/{id}
Update a question by its ID.
Path Parameter:
id
: ID of the question to update Request Body:
{
"title": "Updated Question Title",
"description": "Updated question description.",
"category": "Updated Category"
}
Responses:
200 OK
: Question updated successfully404 Not Found
: Question not found500 Internal Server Error
: Server error
-
DELETE
/questions/{id}
Delete a question by its ID.
Path Parameter:
id
: ID of the question to delete
Responses:
-
200 OK
: Question deleted successfully -
404 Not Found
: Question not found -
500 Internal Server Error
: Server error
-
POST
/answers/{id}/upvote
Upvote a specific answer identified by ID.
Path Parameter:
id
: ID of the answer to upvote
Responses:
-
200 OK
: Successfully upvoted the answer -
404 Not Found
: Answer not found -
500 Internal Server Error
: Server error
-
POST
/answers/{id}/downvote
Downvote a specific answer identified by ID.
Path Parameter:
id
: ID of the answer to downvote
Responses:
-
200 OK
: Successfully downvoted the answer -
404 Not Found
: Answer not found -
500 Internal Server Error
: Server error
-
GET
/answers/{id}
Retrieve an answer by its ID.
Path Parameter:
id
: ID of the answer to retrieve
Responses:
-
200 OK
: Successfully retrieved the answer -
404 Not Found
: Answer not found -
500 Internal Server Error
: Server error
-
PUT
/answers/{id}
Update an answer by its ID.
Path Parameter:
id
: ID of the answer to update
Request Body:
{
"content": "This is an example answer."
}
Responses:
200 OK
: Answer updated successfully404 Not Found
: Answer not found500 Internal Server Error
: Server error
-
DELETE
/answers/{id}
Delete an answer by its ID.
Path Parameter:
id
: ID of the answer to delete
Responses:
-
200 OK
: Answer deleted successfully -
404 Not Found
: Answer not found -
500 Internal Server Error
: Server error
{
"id": "string",
"title": "string",
"description": "string",
"category": "string",
"created_at": "string (date-time)",
"updated_at": "string (date-time)"
}
{
"title": "string",
"description": "string",
"category": "string"
}
{
"id": "string",
"title": "string",
"description": "string",
"category": "string",
"created_at": "string (date-time)",
"updated_at": "string (date-time)",
"upvotes": "integer",
"downvotes": "integer"
}
{
"id": "string",
"question_id": "string",
"content": "string",
"created_at": "string (date-time)",
"updated_at": "string (date-time)"
}
{
"content": "string"
}
{
"id": "string",
"question_id": "string",
"content": "string",
"created_at": "string (date-time)",
"updated_at": "string (date-time)",
"upvotes": "integer",
"downvotes": "integer"
}
- Clone the repository:
git clone <repository-url>
cd <repository-directory>
- Install dependencies:
npm install
- Set up your environment variables as required (database connection, etc.).
- Start the server:
npm start
Ensure the server is running after installation. Use a tool like Postman, Swagger or curl to interact with the API endpoints as documented.