A simple RESTful API for managing jokes.
This API allows you to fetch random jokes, retrieve jokes by ID or type, add new jokes, edit existing jokes, and delete jokes. It is designed for learning purposes and can be tested using tools like Postman.
- Fetch random jokes.
- Retrieve jokes by their ID.
- Filter jokes by their type.
- Add new jokes.
- Edit jokes entirely or partially.
- Delete specific jokes or all jokes with an API key.
-
Clone this repository.
-
Install the required dependencies:
npm install
-
Start the server:
npm start
The server runs on
http://localhost:3000. -
Use Postman or any API client to interact with the endpoints.
Fetch a random joke.
- Example Request:
GET http://localhost:3000/random
- Response:
{ "id": 67, "jokeText": "I'm reading a book about anti-gravity. It's impossible to put down!", "jokeType": "Science" }
Retrieve a specific joke by its ID.
- Example Request:
GET http://localhost:3000/jokes/2
- Response:
{ "id": 2, "jokeText": "Why did the scarecrow win an award? Because he was outstanding in his field.", "jokeType": "Puns" }
Retrieve jokes by type.
-
Query Parameters:
type: Filter by joke type (e.g., "Science", "Puns", "Wordplay", "Food", "Sports", "Movies", "Math").
-
Example Request:
GET http://localhost:3000/filter?type=Science
-
Response:
[ { "id": 1, "jokeText": "Why don't scientists trust atoms? Because they make up everything.", "jokeType": "Science" }, { "id": 5, "jokeText": "Why did the physicist break up with the biologist? There was no chemistry.", "jokeType": "Science" }, { "id": 12, "jokeText": "What do you call an acid with an attitude? A-mean-o acid.", "jokeType": "Science" } ]
Add a new joke.
- Request Body:
{ "text": "What's a computer's favorite snack? Microchips.", "type": "Tech" } - Example Request:
POST http://localhost:3000/jokes
- Response:
{ "message": "Joke added successfully!", "joke": { "id": 101, "jokeText": "What's a computer's favorite snack? Microchips.", "jokeType": "Tech" } }
Replace an existing joke.
- Request Body:
{ "text": "Why don't programmers like nature? It has too many bugs.", "type": "Tech" } - Example Request:
PUT http://localhost:3000/jokes/2
- Response:
{ "id": 2, "jokeText": "Why don't programmers like nature? It has too many bugs.", "jokeType": "Tech" }
Edit specific fields of a joke.
- Request Body:
{ "type": "Wordplay" } - Example Request:
PATCH http://localhost:3000/jokes/1
- Response:
{ "id": 1, "jokeText": "Why don't scientists trust atoms? Because they make up everything.", "jokeType": "Wordplay" }
Delete a specific joke by ID.
- Example Request:
DELETE http://localhost:3000/jokes/2
- Response:
OK
Delete all jokes (requires master key).
- Query Parameters:
key: API key for authentication.
- Example Request:
DELETE http://localhost:3000/all?key=4VGP2DN-6EWM4SJ-N6FGRHV-Z3PR3TT
- Response:
OK
- For POST, PUT, and PATCH requests, ensure to send data in the request body as JSON or URL-encoded format.
- For DELETE
/all, include the master key as a query parameter (key).