This is a simple RESTful API for managing a collection of books. It allows users to perform CRUD (Create, Read, Update, Delete) operations on books stored in a MongoDB database.
The Books API is a simple RESTful API that allows users to manage a collection of books. It provides endpoints for performing CRUD (Create, Read, Update, Delete) operations on books stored in a MongoDB database. The API is built using Node.js and utilizes the Express framework for handling HTTP requests and routing.
Project has been deployed on render.com
To install and run the Sentrimetric API, follow these steps:
- Clone the repository:
git clone <repository-url>
- Install dependencies:
pnpm install
The Books API requires a Mongo database. You can use Docker to run a mongo container or use MongoAtlas Add the database link to the .env file
Start the server using this command
pnpm prod
The API server should now be running and accessible at http://localhost:$PORT
.
Make sure to adjust the Mongo Connection String and PORT details in the .env
file if you are using custom configurations.
One notable addition to the project is the zenv
function, which is used to verify and parse the schema file using Zod. This function ensures that the environment variables are properly defined and validated based on the specified schema.
To run the application using Docker, follow these steps:
- Build the Docker image:
docker build -t books-api .
- Run the docker container
docker run -d -p 3000:1337 books-api
- dotenv: Loads environment variables from a .env file.
- express: Web framework for Node.js.
- helmet: Helps secure Express apps by setting various HTTP headers.
- mongoose: MongoDB object modeling tool.
- pino: Fast and opinionated logger for Node.js.
- pino-pretty: Prettifies Pino logs for better readability.
- uuid: Generates UUIDs (Universally Unique Identifiers).
- zod: TypeScript-first schema validation library.
- @types/express: TypeScript definitions for Express.
- @types/node: TypeScript definitions for Node.js.
- @typescript-eslint/eslint-plugin: ESLint plugin for TypeScript.
- @typescript-eslint/parser: ESLint parser for TypeScript.
- eslint: Pluggable linting utility for JavaScript and TypeScript.
- prettier: Opinionated code formatter.
- tsx: TypeScript watch and build tool.
- typescript: JavaScript compiler that provides TypeScript support.
- pnpm: Fast, disk space efficient package manager.
To run the Books API, you can use the following npm scripts:
-
Development Mode
- Run the API server in development mode with automatic restart on file changes:
pnpm serve
- Run the API server in development mode with automatic restart on file changes:
-
Production Mode
- Build the TypeScript code and start the API server in production mode:
pnpm prod
- Build the TypeScript code and start the API server in production mode:
-
Build
- Build the TypeScript code to the
dist
directory:pnpm build
- Build the TypeScript code to the
-
Create Book
POST /api/books
- Create a new Book
-
Get Books
POST /api/books
- Get all books
-
Get Book
POST /api/books/:id
- Get details about a specific book
-
Filter Books
-
GET /api/books?genre=${somerandomgenre}
-Filter By Genre -
GET /api/books?year=${year}
-Filter By year -
GET /api/books?rating=${rating}
-Filter By rating
-
-
Update Books
PUT /api/books/:id
- Update the book
-
Delete Books
DELETE /api/books/:id
- Update the book