<h4>DELETE /api/books/:id</h4>
<p>Delete a book.</p>
</div>
<script>
const API_URL = '/api';
// Helper function to display results
function displayResults(data) {
document.getElementById('results').innerHTML = '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
}
// Helper function to make API calls
async function apiCall(endpoint, method = 'GET', body = null) {
const options = {
method,
headers: { 'Content-Type': 'application/json' },
};
if (body) options.body = JSON.stringify(body);
Comprehensive Bookstore Frontend with API Documentation
This comprehensive HTML page provides a complete interface for interacting with the bookstore API. Here's a breakdown of its features and how to use it:
Structure:
The page is divided into sections for Authors and Books.
Each section contains forms for different API operations (Create, Read, Update, Delete).
There's a Results section to display API responses.
An API Documentation section provides details on each endpoint.
Authors Section:
Create Author: Fill in the name and bio, then submit to create a new author.
Get All Authors: Click the button to retrieve all authors.
Books Section:
Create Book: Fill in the book details, including the author's ID, to create a new book.
Get All Books: Click the button to retrieve all books.
Get Book by ID: Enter a book ID to retrieve a specific book.
Update Book: Enter a book ID and the fields you want to update, then submit to update the book.
Delete Book: Enter a book ID to delete a specific book.
Results:
After each operation, the API response will be displayed in the Results section.
API Documentation:
Provides details on each API endpoint, including the HTTP method, path, and expected request body.
JavaScript Functionality:
The apiCall function handles all API requests, making it easy to add new operations.
Each form submission is handled by an event listener that calls the appropriate API endpoint.
Results are displayed using the displayResults function.
To use this page:
Save it as an HTML file (e.g., index.html) in your project's public directory.
Ensure your Express app is set up to serve static files. Add this to your main app file:
javascript
Copy
app.use(express.static('public'));
Start your Express server and navigate to this page in your browser.
This interface allows students to:
Understand the structure of API requests and responses.
Test all CRUD operations for both books and authors.
See how data is passed to and received from the API.
Understand the relationship between books and authors (via the author ID).
Important usage notes:
Always create an author before creating a book, as you need the author's ID.
When updating a book, you only need to fill in the fields you want to change.
Pay attention to the API responses in the Results section to understand what data is returned.
Refer to the API Documentation section to understand the structure of each API endpoint.
This comprehensive interface serves as both a learning tool and a testing platform for the bookstore API, allowing students to interact with the API and understand its functionality in a practical, hands-on manner.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (