A simple RESTful API for managing a collection of books built with Express.js.
- CRUD operations for books
- Advanced search and filtering
- Sorting capabilities
- Pagination support
- JSON-based responses
- Input validation
- Error handling
- Node.js (v14 or higher)
- pnpm (or npm/yarn)
- .env file with PORT configuration
-
Clone the repository:
git clone <repository-url> cd express-books-api
-
Install dependencies:
pnpm install
-
Create a
.envfile in the root directory:PORT=3000 -
Start the server:
pnpm start
GET /books
Returns a list of all books. Supports the following query parameters:
search: Search books by title or authorsort: Field to sort by (id,title,author,pages)sortBy: Sort direction (ascordesc)filter: Field to filter by (titleorauthor)filterBy: Value to filter withpage: Page number for paginationpageSize: Number of items per page
Example queries:
/books?sort=pages&sortBy=desc
/books?search=tolkien
/books?filter=author&filterBy=George
/books?page=1&pageSize=10
GET /books/:id
Returns a single book by ID.
POST /books
Creates a new book. Required fields in request body:
title: stringauthor: stringpages: number
PUT /books/:id
Updates a book. Optional fields in request body:
title: stringauthor: stringpages: number
DELETE /books/:id
Deletes a book by ID.
200: Successful GET/PUT request201: Successful POST request204: Successful DELETE request
400: Invalid request body404: Book not found
{
"id": 1,
"title": "1984",
"author": "George Orwell",
"pages": 328
}GET /books?search=tolkien
GET /books?sort=pages&sortBy=desc
GET /books?filter=author&filterBy=George&page=1&pageSize=10
To run the server in development mode:
pnpm devMIT