This project implements a robust Library Management System API using Node.js, Express, and MongoDB. The API offers functionalities to manage admins, books, customers, genres, issued books, and returned books. It utilizes JSON Web Tokens (JWT) for authentication and authorization.
-
Clone the repository:
git clone https://github.com/aliasar1/Library-Management-Api.git
-
Navigate to the project directory:
cd library-management-api
-
Install dependencies:
npm install
-
Set up environment variables:
Create a
.env
file in the root directory and define the following variables:MONGODB_URI=<your_mongodb_uri> JWT_SECRET=<your_jwt_secret>
Replace
<your_mongodb_uri>
with your MongoDB connection string and<your_jwt_secret>
with a secret key for JWT authentication.
Start the API server using either of the following commands:
-
Development mode (with nodemon):
npm run dev
-
Production mode:
npm start
The API will be accessible at http://localhost:8010
.
-
Admins:
/admin/register
(POST): Register a new admin./admin/login
(POST): Login as an admin./admin/current
(GET): Get current admin details.
-
Genres:
/genres
(GET): Get all genres./genres/:id
(GET): Get a specific genre by ID./genres
(POST): Create a new genre./genres/:id
(PUT): Update a genre./genres/:id
(DELETE): Delete a genre.
-
Books:
/books
(GET): Get all books./books/:id
(GET): Get a specific book by ID./books
(POST): Create a new book./books/:id
(PUT): Update a book./books/:id
(DELETE): Delete a book.
-
Customers:
/customers/register
(POST): Register a new customer./customers/login
(POST): Login as a customer./customers/current
(GET): Get current customer details.
-
Issues:
/issues/issueBook
(POST): Issue a book to a customer./issues/issuedBooks
(GET): Get all issued books./issues/customerIssuedBooks
(GET): Get books issued to a specific customer.
-
Returns:
/returns/returnBook
(POST): Return a book./returns/returnedBooks
(GET): Get all returned books./returns/customerReturnedBooks
(GET): Get books returned by a specific customer.
The API includes a centralized error handling middleware (errorHandler
) to handle and format errors consistently.
- Express: Fast, unopinionated, minimalist web framework for Node.js.
- Mongoose: Elegant MongoDB object modeling for Node.js.
- jsonwebtoken: JSON Web Token implementation.
- bcrypt: Library for hashing passwords.
- dotenv: Loads environment variables from a .env file.
- express-async-handler: Simple middleware for handling exceptions inside of async express routes.
Feel free to contribute by opening issues, providing feedback, or submitting pull requests.
Happy coding! 🚀