How to build Library Management System using Node.js?
Last Updated :
29 Jul, 2024
In this article, we are going to create a Library Management System. A Library Management System is basically used to manage the book record of a Library, where a librarian can view all the available books, add new books, delete books, issue books, and return books.
Note: Follow given link to build a Web application on Library Management System.
Functionalities: A librarian can do the following things with this Library Management System:
- Display Available Books
- Add New Books
- Delete Books
- Issue Books
- Return Books
Approach: We are going to use Body Parser by which we can capture user input values from the form such as Book Name, Author Name, Pages and Price & store them in a collection. Then we are going to send the data of the book to the web page using EJS. EJS is a middleware that makes it easy to send data from your server file (app.js or server.js) to a web page. We are also going to create the Delete Route for deleting books, an Issue Route for issuing the books, and a Return Route for returning the books.
Implementation: Below is the step-by-step implementation of the above approach.
Step 1: Project Setup:
Initializes NPM: Create and Locate your project folder into the terminal & type the command
npm init -y
It initializes our node application & makes a package.json file.
Install Dependencies: Locate your root project directory into the terminal and type the command
npm install express ejs body-parser
To install Express, EJS, and Body Parser as dependencies inside your project
Create Server File: Create an ‘app.js’ file, inside this file require the Express Module, and create a constant ‘app’ for creating an instance of the express module, then set the EJS as the default view engine.
const express = require('express');
const app = express();
app.set('view engine', 'ejs');Rearrange Your Directories: It is required to use ‘.ejs’ as an extension for the HTML file instead of ‘.html’ for using EJS inside it. Then you have to move every ‘.ejs’ file in the views directory inside your root directory. EJS is by default looking for ‘.ejs’ files inside the views folder.
Use EJS variable: Inside your updated .ejs file, you have to use EJS Variables to receive values from your server file. You can declare variables in EJS like
<%= variableName %>
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<%= variableName %>
</body>
</html>
Send data to a variable: Inside your server file ( app.js or index.js ), you can send an EJS file along with some data by using the render method.
app.get("/", (req, res) => {
res.render("home", { variableName: "Hello Geeks!" })
})
JavaScript
const express = require('express')
const app = express()
app.set('view engine', 'ejs')
app.get("/", (req, res) => {
res.render("home", { variableName: "Hello Geeks!" })
})
app.listen(3000, (req, res) => {
console.log("App is running on port 3000")
})
Fetching data from form to app.js: To receive input values of a form, we have to use a node package named body-parser.
Install body-parser:
npm install body-parser
Require body-parser module:
const bodyParser = require('body-parser')
And then:
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
extended: true
}));
Then we can handle form data using the request object.
Step 2: Fetch Available Books: We have an array of books with different properties. Let’s send the array to our web page. In the previous step, we just sent a value to the variable, now we are sending the complete array.
JavaScript
const express = require('express')
const bodyParser = require('body-parser')
const 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"
}
]
const app = express()
app.set('view engine', 'ejs')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
app.get("/", function (req, res) {
res.render("home", {
data: books
})
})
app.listen(3000, (req, res) => {
console.log("App is running on port 3000")
})
Since we have so many elements inside our array and we have to print each of them so we have to use For Each Loop to loop through every single element inside our collection and display the details.
HTML
<!DOCTYPE html>
<html>
<head>
<title>ChatRoom</title>
</head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<body>
<h1>All Books</h1>
<table>
<tr>
<th>Book Name</th>
<th>Book Author</th>
<th>Book Pages</th>
<th>Book Price</th>
<th>Book Availability</th>
<th>Issue</th>
<th>Return</th>
<th>Delete</th>
</tr>
<% data.forEach(element=> { %>
<tr>
<td>
<%= element.bookName %>
</td>
<td>
<%= element.bookAuthor %>
</td>
<td>
<%= element.bookPages %>
</td>
<td>
<%= element.bookPrice %>
</td>
<td>
<%= element.bookState %>
</td>
</tr>
<% }) %>
</table>
</body>
</html>
Step 3: Add Books to the list: For this, we have to create a form and handle the form data inside our ‘app.js’ file using Body Parser.
<form action="/" method="post">
<input type="text" placeholder="Book Name"
name="bookName">
<input type="text" placeholder="Book Author"
name="bookAuthor">
<input type="text" placeholder="Book Pages"
name="bookPages">
<input type="text" placeholder="Book Price"
name="bookPrice">
<button type="submit">Add</button>
</form>
Handle form data inside ‘app.js’: We have to fetch values from a form using req.body.valueName, and then arrange it like an object and push it inside our book’s array.
app.post("/", (req, res) => {
const inputBookName = req.body.bookName
const inputBookAuthor = req.body.bookAuthor
const inputBookPages = req.body.bookPages
const inputBookPrice = req.body.bookPrice
books.push({
bookName: inputBookName,
bookAuthor: inputBookAuthor,
bookPages: inputBookPages,
bookPrice: inputBookPrice,
bookState: "Available"
})
res.render("home", {
data: books
})
})
Step 4: Issue Books: Updating Web Page giving an issue option: We have to create a form that sends the book name which we want to issue to the server file ‘app.js’.
<form action="/issue" method="post">
<input type="text" style="display: none;"
name="bookName" value="<%= element.bookName %>">
<button type="submit">Issue</button>
</form>
For issuing a book, we have to create an issue route where we are going to fetch the requested book’s name and search for the element which has the same book name, and change the element state property to Issued.
app.post('/issue', (req, res) => {
var requestedBookName = req.body.bookName;
books.forEach(book => {
if (book.bookName == requestedBookName) {
book.bookState = "Issued";
}
})
res.render("home", {
data: books
})
})
Step 5: Return Books: Updating Web Page giving a return option: We have to create a form that sends the book name which we want to return to the server file ‘app.js’.
<form action="/return" method="post">
<input type="text" style="display: none;"
name="bookName" value="<%= element.bookName %>">
<button type="submit">Return</button>
</form>
For returning a book, we have to create a return route where we are going to fetch the requested book’s name and search for the element which has the same book name, and change the element state property to Available.
app.post('/return', (req, res) => {
var requestedBookName = req.body.bookName;
books.forEach(book => {
if (book.bookName == requestedBookName) {
book.bookState = "Available";
}
})
res.render("home", {
data: books
})
})
Step 6: Delete Books: Updating Web Page giving a delete option: We have to create a form that sends the book name which we want to delete to the server file ‘app.js’.
<form action="/delete" method="post">
<input type="text" style="display: none;"
name="bookName" value="<%= element.bookName %>">
<button type="submit">Delete</button>
</form>
For deleting a book, we have to create a delete route where we are going to fetch the requested book’s name and search for the element which has the same book name, and delete the element.
app.post('/delete', (req, res) => {
var requestedBookName = req.body.bookName;
var j = 0;
books.forEach(book => {
j = j + 1;
if (book.bookName == requestedBookName) {
books.splice((j - 1), 1)
}
})
res.render("home", {
data: books
})
})
Example: Below is the complete code to build Library Management System using Node.js:
app.js
const express = require('express')
const bodyParser = require('body-parser')
const 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"
}
]
const app = express()
app.set('view engine', 'ejs')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
app.get("/", function (req, res) {
res.render("home", {
data: books
})
})
app.post("/", (req, res) => {
const inputBookName = req.body.bookName
const inputBookAuthor = req.body.bookAuthor
const inputBookPages = req.body.bookPages
const inputBookPrice = req.body.bookPrice
books.push({
bookName: inputBookName,
bookAuthor: inputBookAuthor,
bookPages: inputBookPages,
bookPrice: inputBookPrice,
bookState: "Available"
})
res.render("home", {
data: books
})
})
app.post('/issue', (req, res) => {
var requestedBookName = req.body.bookName;
books.forEach(book => {
if (book.bookName == requestedBookName) {
book.bookState = "Issued";
}
})
res.render("home", {
data: books
})
})
app.post('/return', (req, res) => {
var requestedBookName = req.body.bookName;
books.forEach(book => {
if (book.bookName == requestedBookName) {
book.bookState = "Available";
}
})
res.render("home", {
data: books
})
})
app.post('/delete', (req, res) => {
var requestedBookName = req.body.bookName;
var j = 0;
books.forEach(book => {
j = j + 1;
if (book.bookName == requestedBookName) {
books.splice((j - 1), 1)
}
})
res.render("home", {
data: books
})
})
app.listen(3000, (req, res) => {
console.log("App is running on port 3000")
})
home.ejs
<!DOCTYPE html>
<html>
<head>
<title>LMS</title>
</head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<body>
<h1>All Books</h1>
<table>
<tr>
<th>Book Name</th>
<th>Book Author</th>
<th>Book Pages</th>
<th>Book Price</th>
<th>Book Availability</th>
<th>Issue</th>
<th>Return</th>
<th>Delete</th>
</tr>
<% data.forEach(element=> { %>
<tr>
<td>
<%= element.bookName %>
</td>
<td>
<%= element.bookAuthor %>
</td>
<td>
<%= element.bookPages %>
</td>
<td>
<%= element.bookPrice %>
</td>
<td>
<%= element.bookState %>
</td>
<td>
0 <form action="/issue" method="post">
<input type="text" style="display: none;"
name="bookName"
value="<%= element.bookName %>">
<button type="submit">Issue</button>
</form>
</td>
<td>
<form action="/return" method="post">
<input type="text" style="display: none;"
name="bookName"
value="<%= element.bookName %>">
<button type="submit">Return</button>
</form>
</td>
<td>
<form action="/delete" method="post">
<input type="text" style="display: none;"
name="bookName"
value="<%= element.bookName %>">
<button type="submit">Delete</button>
</form>
</td>
</tr>
<% }) %>
</table>
<h1>Add Book</h1>
<form action="/" method="post">
<input type="text" placeholder="Book Name"
name="bookName">
<input type="text" placeholder="Book Author"
name="bookAuthor">
<input type="text" placeholder="Book Pages"
name="bookPages">
<input type="text" placeholder="Book Price"
name="bookPrice">
<button type="submit">Add</button>
</form>
</body>
</html>
Output:
Similar Reads
Web and API Development
How to build Node.js Blog API ?
In this article, we are going to create a blog API using Node.js. A Blog API is an API by which users can fetch blogs, write blogs to the server, delete blogs, and even filter blogs with various parameters. Functionalities: Fetch BlogsCreate BlogsDelete BlogsFilter BlogsApproach: In this project, we
5 min read
RESTful Blogging API with Node and Express.js
Blogs Websites have become very popular nowadays for sharing your thoughts among the users over internet. In this article, you will be guided through creating a Restful API for the Blogging website with the help of Node, Express, and MongoDB. Prerequisites:Node JS & NPMExpress JSMongoDBApproach
6 min read
Build a Social Media REST API Using Node.js: A Complete Guide
Developers build an API(Application Programming Interface) that allows other systems to interact with their Applicationâs functionalities and data. In simple words, API is a set of protocols, rules, and tools that allow different software applications to access allowed functionalities, and data and
15+ min read
Communication and Social Platforms
Management Systems
Customer Relationship Management (CRM) System with Node.js and Express.js
CRM systems are important tools for businesses to manage their customer interactions, both with existing and potential clients. In this article, we will demonstrate how to create a CRM system using Node.js and Express. We will cover the key functionalities, prerequisites, approach, and steps require
15+ min read
Library Management Application Backend
Library Management System backend using Express and MongoDB contains various endpoints that will help to manage library users and work with library data. The application will provide an endpoint for user management. API will be able to register users, authenticate users, borrow books, return books,
10 min read
How to build Library Management System using Node.js?
In this article, we are going to create a Library Management System. A Library Management System is basically used to manage the book record of a Library, where a librarian can view all the available books, add new books, delete books, issue books, and return books. Note: Follow given link to build
9 min read
Student Management System using Express.js and EJS Templating Engine
In this article, we build a student management student which will have features like adding students to a record, removing students, and updating students. We will be using popular web tools NodeJS, Express JS, and MongoDB for the backend. We will use HTML, CSS, and JavaScript for the front end. We'
5 min read
Subscription Management System with NodeJS and ExpressJS
In this article, weâll walk through the step-by-step process of creating a Subscription Management System with NodeJS and ExpressJS. This application will provide users with the ability to subscribe to various plans, manage their subscriptions, and include features like user authentication and autho
5 min read
Building a Toll Road Management System using Node.js
In this article, we are going to build a simple Toll Road Management System using Node.js, where the data will be stored in a local MongoDB database. Problem Statement: In a toll tax plaza, it is difficult to record all the transactions and store them in a single place, along with that, if required,
15+ min read
How to Build User Management System using Node.js ?
In this article, we are going to create a User Management System. A User Management System is used to manage the users of any organization, organizations can View Users, Add Users, Delete Users, and Update Users. Prerequisites:Node JSExpress JSApproach To build a User Management System, we will set
4 min read
User Management System Backend
User Management System Backend includes numerous endpoints for performing user-dealing tasks. The backend could be constructed with the use of NodeJS and MongoDB with ExpressJS . The software will offer an endpoint for consumer management. API will be capable of registering, authenticating, and cont
4 min read
File and Document Handling
Build a document generator with Express using REST API
In the digital age, the need for dynamic and automated document generation has become increasingly prevalent. Whether you're creating reports, invoices, or any other type of document, having a reliable system in place can streamline your workflow. In this article, we'll explore how to build a Docume
2 min read
DOCX to PDF Converter using Express
In this article, we are going to create a Document Conversion Application that converts DOCX to PDF. We will follow step by step approach to do it. We also make use of third-party APIs. Prerequisites:Express JS multernpm Preview of the final output: Let us have a look at how the final output will lo
4 min read
How to Send Email using Node.js ?
Sending emails is a common task in many applications, whether it be for sending notifications, account verification, password resets, or other communication needs. In this article, we'll explore how to send emails using Node.js. We will cover setting up a mail transport, composing the email, and sen
8 min read
File Sharing Platform with Node.js and Express.js
In today's digital age, the need for efficient File sharing platforms has become increasingly prevalent. Whether it's sharing documents for collaboration or distributing media files, having a reliable solution can greatly enhance productivity and convenience. In this article, we'll explore how to cr
4 min read
React Single File Upload with Multer and Express.js
When we want to add functionality for uploading or deleting files, file storage becomes crucial, whether it's for website or personal use. The File Storage project using Express aims to develop a web application that provides users with a secure and efficient way to store and manage their files onli
5 min read
Task and Project Management
Task Management System using Node and Express.js
Task Management System is one of the most important tools when you want to organize your tasks. NodeJS and ExpressJS are used in this article to create a REST API for performing all CRUD operations on task. It has two models User and Task. ReactJS and Tailwind CSS are used to create a frontend inter
15+ min read
Task Manager App using Express, React and GraphQL.
The Task Manager app tool is designed to simplify task management with CRUD operation: creation, deletion, and modification of tasks. Users can easily generate new tasks, remove completed ones, and update task details. In this step-by-step tutorial, you will learn the process of building a Basic Tas
6 min read
Simple Task Manager CLI using Node.js
Before creating a simple Task Manager CLI using NodeJS, let's run our first basic Node.js application with the following simple steps: Note: You can download and install the Node.js application from href="https://nodejs.org/en/" Step 1: Create a Directory for our Task Manager CLI named TODO-CLI, you
4 min read
Task Scheduling App with Node and Express.js
Task Scheduling app is an app that can be used to create, update, delete, and view all the tasks created. It is implemented using NodeJS and ExpressJS. The scheduler allows users to add tasks in the cache of the current session, once the app is reloaded the data gets deleted. This can be scaled usin
4 min read
Todo List CLI application using Node.js
CLI is a very powerful tool for developers. We will be learning how to create a simple Todo List application for command line. We have seen TodoList as a beginner project in web development and android development but a CLI app is something we don't often hear about. Pre-requisites:A recent version
13 min read