A RESTful API built with Express, TypeScript, and MongoDB for managing books, authors, and categories in a digital library system.
- Full CRUD operations for Books, Authors, and Categories
- MongoDB with Mongoose for data persistence
- TypeScript for type safety
- File upload support for book cover images (Multer)
- Advanced filtering for books (by author, categories, title)
- Soft delete functionality for books
- Automatic timestamps (createdAt, updatedAt) on all models
- Request logging with Morgan
- CORS support
- Custom error handling
- Node.js (v14 or higher)
- MongoDB (local or remote instance)
- npm or yarn
- Clone the repository:
git clone <repository-url>
cd Books-Management-API-Coded- Install dependencies:
npm install- Create a
.envfile in the root directory:
PORT=3000
MONGODB_URI=mongodb://localhost:27017/books-management
NODE_ENV=development- Start MongoDB (if running locally)
npm run devnpm run build
npm startnpm run type-checkThe server will start on http://localhost:3000 (or the port specified in .env).
http://localhost:3000/api
GET /api/authors- Get all authors (with populated books)GET /api/authors/:id- Get author by IDPOST /api/authors- Create new authorPUT /api/authors/:id- Update authorDELETE /api/authors/:id- Delete author
Create Author Example:
{
"name": "J.K. Rowling",
"country": "United Kingdom"
}GET /api/categories- Get all categoriesGET /api/categories/:id- Get category by IDPOST /api/categories- Create new categoryPUT /api/categories/:id- Update category nameDELETE /api/categories/:id- Delete category
Create Category Example:
{
"name": "Fantasy"
}GET /api/books- Get all books (with filtering options)GET /api/books/:id- Get book by IDPOST /api/books- Create new book (with optional cover image)PUT /api/books/:id- Update book (with optional cover image)DELETE /api/books/:id- Soft delete book
Create Book Example (JSON):
{
"title": "Harry Potter and the Philosopher's Stone",
"author": "author_id_here",
"categories": ["category_id1", "category_id2"]
}Create Book with Cover Image:
Use multipart/form-data with:
title(text)author(text - author ID)categories(text - JSON array string)coverImage(file - image file)
The GET /api/books endpoint supports query parameters:
?author=<authorId>- Filter by author ID?categories=<categoryId>- Filter by single category ID?categories=<id1,id2,id3>- Filter by multiple categories (comma-separated)?title=<partialTitle>- Case-insensitive title search?includeDeleted=true- Include soft-deleted books
Examples:
GET /api/books?author=123
GET /api/books?categories=456,789
GET /api/books?title=harry
GET /api/books?author=123&title=harry&categories=456,789
GET /api/books?includeDeleted=true
name(string, required)country(string, required)books(array of ObjectId references)createdAt(Date, automatic)updatedAt(Date, automatic)
name(string, required, unique)books(array of ObjectId references)createdAt(Date, automatic)updatedAt(Date, automatic)
title(string, required)author(ObjectId reference to Author, required)categories(array of ObjectId references to Category)coverImage(string, optional - filename)deleted(boolean, default: false)createdAt(Date, automatic)updatedAt(Date, automatic)
Book cover images can be uploaded when creating or updating books:
- Supported formats: JPEG, JPG, PNG, GIF
- Maximum file size: 5MB
- Files are stored in the
uploads/directory - Access uploaded images via:
http://localhost:3000/uploads/filename.jpg
Books support soft delete functionality:
- When a book is deleted, the
deletedfield is set totrue - Soft-deleted books are excluded from GET queries by default
- Use
?includeDeleted=trueto include soft-deleted books in results - The document is not removed from the database
The API returns consistent error responses:
{
"success": false,
"error": "Error message here"
}Common HTTP status codes:
200- Success201- Created400- Bad Request404- Not Found500- Internal Server Error
A Postman collection is included: Books-Management-API.postman_collection.json
Import this file into Postman to test all endpoints with example requests.
Books-Management-API-Coded/
├── src/
│ ├── config/
│ │ └── database.ts
│ ├── controllers/
│ │ ├── authorController.ts
│ │ ├── categoryController.ts
│ │ └── bookController.ts
│ ├── middlewares/
│ │ ├── errorHandler.ts
│ │ ├── notFoundHandler.ts
│ │ └── upload.ts
│ ├── models/
│ │ ├── Author.ts
│ │ ├── Category.ts
│ │ └── Book.ts
│ ├── routes/
│ │ ├── authorRoutes.ts
│ │ ├── categoryRoutes.ts
│ │ ├── bookRoutes.ts
│ │ └── index.ts
│ ├── app.ts
│ └── server.ts
├── uploads/
├── .env.example
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
- Express - Web framework
- TypeScript - Type-safe JavaScript
- MongoDB - Database
- Mongoose - MongoDB ODM
- Morgan - HTTP request logger
- CORS - Cross-origin resource sharing
- Multer - File upload handling
- dotenv - Environment variable management
ISC