The Book Management API is a Node.js-based RESTful API that provides functionalities for managing book entries. It offers user authentication, CRUD operations for book management, and filtering books by author or publication year.
- User authentication using login and signup endpoints.
- CRUD operations for managing book entries (Create, Read, Update, Delete).
- Filtering books by author or publication year.
- Implementation of basic security measures including input validation.
-
Node.js: A JavaScript runtime environment that executes JavaScript code outside a web browser. Used as the backend runtime for the API.
-
Express.js: A minimalist web framework for Node.js that provides a robust set of features for building web and mobile applications. Used for handling routing, middleware, and request/response processing.
-
bcryptjs: A library for hashing passwords using bcrypt, a cryptographic hash function designed to securely hash passwords. Used for password hashing to enhance security.
-
dotenv: A zero-dependency module that loads environment variables from a
.env
file intoprocess.env
. Used for managing sensitive configuration data such as port numbers and database credentials.
- Node.js and npm installed on your machine.
-
Clone this repository to your local machine:
git clone https://github.com/your-username/book-management-api.git
-
Navigate to the project directory:
cd book-management-api
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and specify the environment variables:PORT=3000
-
Start the server:
npm start
-
Use an API testing tool like Postman to interact with the API endpoints.
POST /login
: User login.POST /signup
: User signup.
The API uses JWT (JSON Web Tokens) for user authentication. To access protected routes, include the JWT token in the Authorization header of the request:
Authorization: Bearer <JWT_TOKEN>
- Input validation is implemented using express-validator to prevent common security vulnerabilities like SQL injection and XSS attacks.
- Environment variables are used to store sensitive information like database URI and JWT secret key.
GET /
: Get all books.GET /:id
: Get a book by ID.POST /
: Create a new book.PUT /:id
: Update an existing book.DELETE /:id
: Delete a book by ID.
GET /filter?author=:author
: Filter books by author.GET /filter?year=:year
: Filter books by publication year.
PORT
: Port number for the server (default is 3000).
Including instructions on how to use Postman to interact with the API endpoints:
- POST /login: Endpoint for user login. Requires
email
andpassword
in the request body. - POST /signup: Endpoint for user signup. Requires
email
,password
, andname
in the request body.
- GET /: Retrieve all books.
- GET /:id: Retrieve a book by its ID.
- POST /: Create a new book. Requires
title
,author
, andpublicationYear
in the request body. - PUT /:id: Update a book by its ID. Requires
title
,author
, andpublicationYear
in the request body. - DELETE /:id: Delete a book by its ID.
- GET /api/books/filter: Filter books by author and/or publication year. Supports query parameters
author
andyear
.
book-management-api/
├── endpoints/
| └── bookServices.js
├── middleware/
├── model/
| ├── authentication.js
| ├── book.js
| ├── db.js
| └── user.js
├── services/
| ├── auth.js
| └── bookService.js
├── .env
├── .gitignore
├── book-magement-api.postman_collection.json
├── package.json
├── readme.md
└── server.js
- Download and install Postman.
- Import the provided Postman Collection.
- Set up environment variables in Postman for
BASE_URL
andTOKEN
(if authentication is enabled). - Start testing the API endpoints.
You can find the Postman Collection for this API in this repo JSON FILe.