Book Management API
A basic CRUD (Create, Read, Update, Delete) API for managing a simple "Books" collection. This project uses Node.js, Express.js, TypeScript, MongoDB, and implements unit and integration tests.
Table of Contents
- Prerequisites
- Installation
- Project Structure
- API Endpoints
- Environment Variables
- Running the Application
- Testing
- File Upload Middleware
- Error Handling
- License
Prerequisites
- Node.js (version 14.x or higher)
- npm (version 6.x or higher)
- MongoDB (running instance)
Installation
url: https://book-management-api-qkeh.onrender.com
-
Clone the repository:
git clone https://github.com/Silvyjson/Book-Management-API.git
-
Navigate to the project directory:
cd book-management-api -
Install dependencies:
npm install
Project Structure
book-management-api/
│
├── src/
│ ├── controllers/
│ │ ├── book_controller.ts
│ ├── models/
│ │ ├── book_model.ts
│ ├── routes/
│ │ ├── book_routes.ts
│ ├── index.ts
│
├── tests/
│ ├── book_controller.test.ts
│
├── .gitignore
├── package.json
├── tsconfig.json
├── README.md
└── .env.example
API Endpoints
Create a Book
- Endpoint:
POST /books - Description: Create a new book.
- Request Body:
{ "title": "Book Title", "author": "Author Name", "publishedDate": "2023-01-01", "ISBN": "123-456-789" } - Response:
{ "message": "The new book is uploaded successfully", "newBook" }
Update Book Cover Picture
- Endpoint:
PATCH /books/cover-image/:id - Description: Update the cover picture of a book.
- Request Body:
{ "coverImageUrl": "http://example.com/cover.jpg" } - Response:
{ "message": "Book cover updated successfully", "updatedBook" }
Get All Books
- Endpoint:
GET /books - Description: Retrieve a list of all books.
- Response:
{ "books" }
Get a Single Book
- Endpoint:
GET /books/:id - Description: Retrieve a single book by its ID.
- Response:
{ "book" }
Update a Book
- Endpoint:
PUT /books/:id - Description: Update a book's details.
- Request Body:
{ "title": "Updated Title", "author": "Updated Author", "publishedDate": "2023-01-01", "ISBN": "123-456-789", "coverImageUrl": "http://example.com/cover.jpg" } - Response:
{ "message": "Book updated successfully", "updatedBook" }
Delete a Book
- Endpoint:
DELETE /books/:id - Description: Delete a book by its ID.
- Response:
{ "message": "Book deleted successfully" }
Environment Variables
Create a .env file in the root directory and add the following variables:
MONGO_URI=mongodb://localhost:27017/book_management
PORT=3000
Running the Application
-
Compile TypeScript files to JavaScript:
npm run build
-
Start the server:
npm srun dev
The application will be running on http://localhost:3000.
Testing
Run the tests using the following command:
npm testError Handling
Basic error handling is implemented to handle invalid requests and non-existent resources. Errors are returned in a structured JSON format with appropriate status codes.
License
This project is licensed under the MIT License.