This document provides a comprehensive guide to the RESTful API for managing books. The API allows you to perform CRUD operations (Create, Read, Update, Delete) on books stored in a MySQL database.
- Node.js and npm are installed on your system.
- A running MySQL or MariaDB instance.
.env.local
file with the following variables:DB_HOST=your_database_host DB_USER=your_database_user DB_PASSWORD=your_database_password DB_DATABASE=your_database_name
- Clone the repository or copy the code files.
- Navigate to the project directory.
- Install dependencies by running:
npm install
- Start the server:
node app.js
- The server will run at
http://localhost:3000
.
GET /api/buku
- Description: Fetches all books from the database.
- Response:
- 200: Returns an array of books.
- 500: If there is an issue with the database.
GET /api/buku/:id
- Description: Fetches a single book by its ID.
- Parameters:
id
(path parameter): The ID of the book.
- Response:
- 200: Returns the book details.
- 404: If the book is not found.
POST /api/buku
- Description: Adds a new book to the database.
- Request Body:
{ "judul": "string", "pengarang": "string", "tahun": "number", "jumlah": "number" }
- Response:
- 200: Returns a success message and the ID of the new book.
- 500: If there is an issue with the database.
PUT /api/buku/:id
- Description: Updates the details of a book.
- Parameters:
id
(path parameter): The ID of the book.
- Request Body:
{ "judul": "string", "pengarang": "string", "tahun": "number", "jumlah": "number" }
- Response:
- 200: Success message.
- 500: If there is an issue with the database.
PATCH /api/buku/:id
- Description: Partially updates details of a book.
- Request Body:
Similar to
PUT
but allows partial fields.
DELETE /api/buku/:id
- Description: Deletes a book from the database.
- Parameters:
id
(path parameter): The ID of the book.
- Response:
- 200: Success message.
- 500: If there is an issue with the database.
curl -X GET http://localhost:3000/api/buku
curl -X GET http://localhost:3000/api/buku/1
curl -X POST http://localhost:3000/api/buku \
-H "Content-Type: application/json" \
-d '{"judul":"Book Title","pengarang":"Author Name","tahun":2023,"jumlah":10}'
curl -X PUT http://localhost:3000/api/buku/1 \
-H "Content-Type: application/json" \
-d '{"judul":"Updated Title","pengarang":"Updated Author","tahun":2024,"jumlah":15}'
curl -X DELETE http://localhost:3000/api/buku/1
- 500 Internal Server Error: Indicates an issue with the database or server logic.
- 404 Not Found: Returned when a book with the specified ID does not exist.
- 400 Bad Request: Returned when the request body is invalid.
- Ensure the database and its table are properly configured before starting the server.
- Example database schema for the
buku
table:CREATE TABLE buku ( id INT AUTO_INCREMENT PRIMARY KEY, judul VARCHAR(255), pengarang VARCHAR(255), tahun INT, jumlah INT );