Build Library Management System Using NodeJS (original) (raw)
Last Updated : 30 Mar, 2026
A Library Management System is an essential application for managing books, users, and transactions in a library. It involves adding, removing, updating, and viewing books and managing users.
Core Features of Web Application
Create a simple web application where administrators can manage books and users. The application will feature:
- Add new books with all the required details.
- View the list of books including details like name, author, pages, prices, and availability.
- Issue and return books (change the book’s availability).
- Delete books from the library.
Approach
Below are the approaches for building the Library Management System using Node.js:
- **Set Up Middleware with EJS: Configure EJS (Embedded JavaScript) as the templating engine to render dynamic pages and display data.
- **Install and Configure Body Parser: Capture form data and handle POST requests from users.
- **Create Routes for Adding Books and Users: Handle form submissions to add new entries to the collection.
- **Create Delete Route: Remove books or users using a unique identifier.
- **Create Update Route: Modify existing book or user details and reflect changes on the UI.
Steps to Build a Library Management System
Follow these steps to design and implement a basic library management system using Node.js and related technologies.
Step 1: Create a Project Folder
Open your terminal (Command Prompt/PowerShell) and run the following commands:
mkdir library-management-system cd library-management-system
Step 2: Initialize a NodeJS Project
Run the following command to create a package.json file:
npm init -y
Step 3: Install Dependencies
Run the following command to install the required dependencies:
npm install express ejs body-parser
This installs
- **Express: Backend framework.
- **EJS: Templating engine.
- **Body-parser: To handle form submissions.
Step 4: Create Server File
Create a file named app.js and require the Express module. Then, create an Express instance and set EJS as the default view engine.
JavaScript ``
const express = require('express'); const bodyParser = require('body-parser'); const app = express(); const PORT = 3000;
// Sample Book Data let books = [ { bookName: "Rudest Book Ever", bookAuthor: "Shwetabh Gangwar", bookPages: 200, bookPrice: 240, bookState: "Available" }, { bookName: "Do Epic Shit", bookAuthor: "Ankur Wariko", bookPages: 200, bookPrice: 240, bookState: "Available" } ];
// Middleware app.set('view engine', 'ejs'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true }));
// Home Route - Display Books app.get("/", (req, res) => { res.render("home", { data: books }); });
// Add Book Route app.post("/", (req, res) => { const newBook = { bookName: req.body.bookName, bookAuthor: req.body.bookAuthor, bookPages: req.body.bookPages, bookPrice: req.body.bookPrice, bookState: "Available" };
books.push(newBook);
res.render("home", { data: books });});
// Issue Book Route app.post("/issue", (req, res) => { const requestedBookName = req.body.bookName; books.forEach(book => { if (book.bookName === requestedBookName) { book.bookState = "Issued"; } }); res.render("home", { data: books }); });
// Return Book Route app.post("/return", (req, res) => { const requestedBookName = req.body.bookName; books.forEach(book => { if (book.bookName === requestedBookName) { book.bookState = "Available"; } }); res.render("home", { data: books }); });
// Delete Book Route app.post("/delete", (req, res) => { const requestedBookName = req.body.bookName; books = books.filter(book => book.bookName !== requestedBookName); res.render("home", { data: books }); });
// Start Server
app.listen(PORT, () => {
console.log(App is running on port ${PORT});
});
``
- **Body-parser middleware: Parses incoming request data and makes it accessible via req.body.
- **EJS view engine: Enables dynamic rendering of HTML templates with server-side data.
- **Books array: Stores book objects with properties like name, author, pages, price, and availability.
- **Root route (/): Renders home.ejs and passes the books array for dynamic display.
- **POST route (/): Captures form data, adds a new book to the array, and re-renders the updated list.
Step 5: Set Up Views Directory
Create a views folder in your root directory and inside it, create a file called home.ejs.
Step 6: Create the Home Page (home.ejs)
Inside views/home.ejs, add the following code
HTML `
Library Management System
All Books
| Book Name | Book Author | Book Pages | Book Price | Book Availability | Issue | Return | Delete |
|---|---|---|---|---|---|---|---|
| <%= element.bookName %> | <%= element.bookAuthor %> | <%= element.bookPages %> | <%= element.bookPrice %> | <%= element.bookState %> | <% if (element.bookState === "Available") { %> Issue <% } %> | <% if (element.bookState === "Issued") { %> Return <% } %> | Delete |
Add Book
Add`
**Output
node app.js