A RESTful API for managing a virtual bookshelf, built with Node.js and Hapi framework. This API allows you to create, read, update, and delete books, as well as filter books by various criteria.
- Store and manage books with rich metadata
- Create new books with validation
- Retrieve all books with optional filtering
- Get detailed information about specific books
- Update existing books
- Delete books
- In-memory data storage (no database setup required)
- Node.js (LTS 18.13.0 or newer recommended)
- npm (comes with Node.js)
-
Clone this repository or download the source code:
git clone https://github.com/kyyril/bookshelf-api.git cd bookshelf-api -
Install dependencies:
npm install
Start the server:
npm run startFor development with automatic restart on file changes:
npm run start-devThe server will run on http://localhost:9000.
Check code quality with ESLint:
npm run lint- Method: POST
- URL:
/books - Request Body:
{ "name": "Book Name", "year": 2023, "author": "Author Name", "summary": "Book summary", "publisher": "Publisher Name", "pageCount": 200, "readPage": 50, "reading": true } - Success Response (201):
{ "status": "success", "message": "Buku berhasil ditambahkan", "data": { "bookId": "unique-id" } }
- Method: GET
- URL:
/books - Query Parameters:
name: Filter by book name (case insensitive)reading: Filter by reading status (1 = reading, 0 = not reading)finished: Filter by finished status (1 = finished, 0 = not finished)
- Success Response (200):
{ "status": "success", "data": { "books": [ { "id": "unique-id", "name": "Book Name", "publisher": "Publisher Name" } ] } }
- Method: GET
- URL:
/books/{bookId} - Success Response (200):
{ "status": "success", "data": { "book": { "id": "unique-id", "name": "Book Name", "year": 2023, "author": "Author Name", "summary": "Book summary", "publisher": "Publisher Name", "pageCount": 200, "readPage": 50, "finished": false, "reading": true, "insertedAt": "timestamp", "updatedAt": "timestamp" } } }
- Method: PUT
- URL:
/books/{bookId} - Request Body: Same as POST
- Success Response (200):
{ "status": "success", "message": "Buku berhasil diperbarui" }
- Method: DELETE
- URL:
/books/{bookId} - Success Response (200):
{ "status": "success", "message": "Buku berhasil dihapus" }
- Status Code: 400
- Response:
{ "status": "fail", "message": "Gagal menambahkan buku. Mohon isi nama buku" }
- Status Code: 400
- Response:
{ "status": "fail", "message": "Gagal menambahkan buku. readPage tidak boleh lebih besar dari pageCount" }
- Status Code: 404
- Response:
{ "status": "fail", "message": "Buku tidak ditemukan" }
curl -X POST http://localhost:9000/books \
-H "Content-Type: application/json" \
-d '{
"name": "Harry Potter and the Philosopher'\''s Stone",
"year": 1997,
"author": "J.K. Rowling",
"summary": "The story of a young wizard who discovers his magical heritage on his eleventh birthday.",
"publisher": "Bloomsbury",
"pageCount": 223,
"readPage": 100,
"reading": true
}'curl http://localhost:9000/bookscurl "http://localhost:9000/books?name=potter&reading=1"bookshelf-api/
├── src/
│ ├── handler.js # Request handlers for all endpoints
│ ├── routes.js # API route definitions
│ ├── server.js # Server configuration and startup
│ └── books.js # In-memory data store
├── .eslintrc.json # ESLint configuration
├── package.json # Project metadata and dependencies
└── README.md # This file
This project is open source and available under the MIT License.