This is a RESTful API for managing books. It allows users to perform CRUD (Create, Read, Update, Delete) operations on books. The API is built using Node.js, Express.js, and MongoDB with Mongoose.
- Endpoint:
POST /api/books
- Usage: Create a new book by sending a POST request with a JSON body containing the book's title, author, and summary.
Example Request:
POST /api/books
{
"title": "Sample Book",
"author": "Sample Author",
"summary": "This is a sample book summary."
}
- Endpoint:
GET /api/books
- Usage: Retrieve a list of all books.
- Endpoint:
GET /api/books/:id
- Usage: Retrieve details of a specific book by providing its unique ID as a URL parameter.
Example Request:
GET /api/books/12345 (where 12345 is the book's ID)
- Endpoint:
PUT /api/books/:id
- Usage: Update a book's details by sending a PUT request with a JSON body containing the updated information.
Example Request:
PUT /api/books/12345 (where 12345 is the book's ID)
{
"title": "Updated Book Title",
"author": "Updated Author",
"summary": "This is the updated book summary."
}
- Endpoint:
DELETE /api/books/:id
- Usage: Delete a book by providing its unique ID as a URL parameter.
Example Request:
DELETE /api/books/12345 (where 12345 is the book's ID)
-
Clone this repository to your local machine:
git clone https://github.com/yourusername/book-api.git cd book-api
-
Install the project dependencies:
npm install
-
Configure MongoDB:
- Create a MongoDB database.
- Update the MongoDB connection string in
index.js
.
-
Start the application:
node index.js
-
The API will be accessible at
http://localhost:3000/api/books
.
- The API assumes that book data includes a title(required), author, and summary.
- MongoDB is used as the database, and Mongoose is the ODM.
- The API uses proper error handling and validation for scenarios like attempting to access or modify non-existent books.
- For security, you should consider implementing authentication and authorization mechanisms in a production environment.