Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Library Management System – Backend API

A RESTful backend for a Library Management System with role-based authentication for Librarians and Members.

Features

  • JWT-based authentication
  • Role-based access control (Librarian / Member)
  • Book management (CRUD) — Librarian only
  • Member management — Librarian only
  • Borrow / Return books — Member only
  • Input validation using express-validator
  • Centralized error handling

Tech Stack

  • Node.js + Express.js
  • MongoDB + Mongoose
  • JWT (jsonwebtoken)
  • bcrypt
  • express-validator

Project Structure

library-management-system/
├── config/
│   └── db.js
├── controllers/
│   ├── authController.js
│   ├── bookController.js
│   └── memberController.js
├── middleware/
│   ├── authMiddleware.js
│   ├── roleMiddleware.js
│   └── errorMiddleware.js
├── models/
│   ├── User.js
│   ├── Book.js
│   └── Borrow.js
├── routes/
│   ├── authRoutes.js
│   ├── bookRoutes.js
│   └── memberRoutes.js
├── validators/
│   └── validationRules.js
├── .env
├── server.js
└── package.json

Installation & Setup

  1. Clone the repository
git clone https://github.com/Pavanakv/Library-Management-System.git
cd library-management-system
  1. Install dependencies
npm install
  1. Create a .env file in the root with the following variables:
PORT=5000
DATABASE_URL=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret_key
  1. Run the server
npm run dev

Server will start on http://localhost:5000

Database Setup

This project uses MongoDB. You can use either:

  • A local MongoDB instance, or
  • MongoDB Atlas (cloud)

Three collections are created automatically by Mongoose on first use:

  • users — stores both members and librarians
  • books — stores book inventory
  • borrows — stores borrow/return records

Note: Librarian accounts cannot be created via the API. They must be inserted directly into the database with a bcrypt-hashed password and role: "librarian".

API Documentation

Auth Routes

Method Endpoint Access Description
POST /api/auth/register Public Register as a member
POST /api/auth/login Public Login (member or librarian)

Book Routes

Method Endpoint Access Description
POST /api/books Librarian Add a new book
GET /api/books Authenticated Get all books
GET /api/books/:id Authenticated Get book details
PUT /api/books/:id Librarian Update book
DELETE /api/books/:id Librarian Delete book
POST /api/books/:id/borrow Member Borrow a book
POST /api/books/:id/return Member Return a book

Member Routes

Method Endpoint Access Description
GET /api/members Librarian Get all members
DELETE /api/members/:id Librarian Delete a member
GET /api/members/me/books Member Get logged-in member's borrowed books

Authentication Flow

  1. Member registers via /api/auth/register (role is always set to member)
  2. User logs in via /api/auth/login, receives a JWT token
  3. Token must be sent in the Authorization header as Bearer <token> for all protected routes
  4. authMiddleware.js verifies the token and attaches the user to req.user
  5. roleMiddleware.js checks req.user.role against the allowed roles for that route

Deployment

Live API URL: https://library-management-system-h8xl.onrender.com

Author

Pavana K V

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages