- Javascript
- NodeJs (Version 18)
- Express
- Mongodb
- Mongoose
- Nodemon and dotenv as developer tool
- Install dependencies with
npm install
- Initialize the server:
npm run dev
- Test the endpoint in Postman or similar:
http://localhost:3000
- Replace the .env.example with .env and update it with your environment variables according to your machine and configurations.
Returns a list of all books.
-
URL
/books
-
Method:
GET
-
Successful Response:
- Code: 200
- Content:
[ { "_id": "65a0299f6e610a5cd290bbec", "title": "Harry Potter", "publisher": "Classics", "price": 10, "pages": 240 }, // Other books... ]
Returns a specific book based on the provided ID.
-
URL
/books/:id
-
Method:
GET
-
URL Parameters:
id
(integer) - Unique ID of the book to be retrieved.
-
Successful Response:
- Code: 200
- Content:
{ "_id": "65a0299f6e610a5cd290bbec", "title": "Harry Potter", "publisher": "Classics", "price": 10, "pages": 240 }
Registers a new book in the database.
-
URL
/books
-
Method:
POST
-
Request Body:
{ "title": "Harry Potter", "publisher": "Classics", "price": 10, "pages": 240 }
Successful Response:
- Code: 201 (Created)
- Content:
{
"id": "65a0299f6e610a5cd290bbec",
"title": "Harry Potter",
"publisher": "Classics",
"price": 10,
"pages": 240
}
Updates the information of a specific book based on the provided ID.
-
URL
/books/:id
-
Method:
PUT
-
URL Parameters:
id
(integer) - Unique ID of the book to be updated.
-
Request Body:
{
"title": "Harry Potter New",
"publisher": "Classics",
"price": 10,
"pages": 270
}
Successful Response:
- Code: 200
- Content:
{
"title": "Harry Potter New",
"publisher": "Classics",
"price": 10,
"pages": 270
}
Deletes a specific book based on the provided ID.
-
URL
/books/:id
-
Method:
DELETE
-
URL Parameters:
id
(string) - Unique ID of the book to be deleted.
-
Successful Response:
- Code: 204 (No Content)
- Content: None
-
Error Response:
- Code: 500 (Internal Server Error)
- Content:
{
"message": "Failed to delete the book"
}
-
URL:
/authors
-
Method:
GET
-
Successful Response:
-
Code: 200
-
Content:
[ { "id": "aspasldaspldq123", "name": "Author name", "nationality": "American" }, // Other authors... ]
-
-
URL:
/authors/:id
-
Method:
GET
-
URL Parameters:
id
(integer) - Unique ID of the author to be retrieved.
-
Successful Response:
- Code: 200
- Content:
{ "id": "aspasldaspldq123", "name": "Author name", "nationality": "American" },
-
URL:
/authors
-
Method:
POST
-
Request Body:
{ "name": "Jane Smith", "nationality": "British" }
-
Successful Response:
- Code: 201 (Created)
- Content:
{ "id": "dkqkeqkdk213sdfmatt8", "name": "Jane Smith", "nationality": "British" }
-
URL:
/authors/:id
-
Method:
PUT
-
URL Parameters:
id
(integer) - Unique ID of the author to be updated.
-
Request Body:
{ "nationality": "Canadian" }
-
Successful Response:
- Code: 200
- Content:
{ "id": "dkqkeqkdk213sdfmatt8", "name": "Jane Smith", "nationality": "Canadian" }
-
URL:
/authors/:id
-
Method:
DELETE
-
URL Parameters:
id
(integer) - Unique ID of the author to be deleted.
-
Successful Response:
- Code: 204 (No Content)
- Content: None
-
Error Response:
-
Code: 500 (Internal Server Error)
-
Content:
{ "message": "Failed to delete the author" }
-