How to Build Hospital Management System using Node.js?
Last Updated :
24 Jul, 2024
In this article, we are going to create a Hospital Management System. A Hospital Management System is basically used to manage patients in the hospital. It is helpful to see which patients do not have a bed allotted or if there are any free beds or not. It makes sure that the discharged patients' beds should not be free, they should be allotted to those who need them.
Functionalities
A Hospital can do the following things with this Hospital Management System:
- Display All Patients
- Add New Patients
- Do not Add New Patients if beds are not available
- Discharge Patients
Approach
We are going to use Body Parser by which we can capture user input values from the form such as the patients' name, number, date of birth, city, phone number, and room number & store them in a collection. Then we will send the patients' data 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 will also create the Discharge Route for discharging the patients.
Installation Steps
Step 1: Make a folder structure for the project.
mkdir myapp
Step 2: Navigate to the project directory
cd myapp
Step 3: Initialize the NodeJs project inside the myapp folder.
npm init -y
Step 4: Install the required dependencies by the following command:
npm install express ejs body-parser
The updated dependencies in package.json file will look like:
"dependencies": {
"body-parser": "^1.20.2",
"ejs": "^3.1.10",
"express": "^4.19.2"
}
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');
We also create a constant availableBeds, and set it to the number of beds available.
const availableBeds = 2;
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.
Fetch Patients Records
We have an array of patients 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
// app.js
const express = require('express')
const bodyParser = require('body-parser')
const patients = [{
name: 'Aditya',
number: '8175826846',
dob: '29/09/2001',
city: 'Mirzapur',
roomNo: '1',
}]
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: patients
})
})
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
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<title>HMS</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 Patients</h1>
<table>
<tr>
<th>Name</th>
<th>Number</th>
<th>DOB</th>
<th>City</th>
<th>Room No.</th>
</tr>
<% data.forEach(element=> { %>
<tr>
<td>
<%= element.name %>
</td>
<td>
<%= element.number %>
</td>
<td>
<%= element.dob %>
</td>
<td>
<%= element.city %>
</td>
<td>
<%= element.roomNo %>
</td>
</tr>
<% }) %>
</table>
</body>
</html>
Add Patients 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="Name" name="name">
<input type="number" placeholder="Number" name="number">
<input type="text" placeholder="DOB" name="dob">
<input type="text" placeholder="City" name="city">
<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 patient's array.
app.post("/", (req, res) => {
const name = req.body.name
const number = req.body.number
const dob = req.body.dob
const city = req.body.city
if (patients.length < availableBeds) {
const roomNo = patients.length + 1;
patients.push({
name: name,
number: number,
dob: dob,
city: city,
roomNo: roomNo
})
res.render("home", {
data: patients
})
}
else {
res.send("No room available");
}
})
We only push the patients if there are any free beds, if not then simply return the No room available message.
Discharge Patients
Updating Web Page giving a Discharge option: We have to create a form that sends the patient's name which we want to Discharge to the server file 'app.js'.
<form action="/https/www.geeksforgeeks.org/discharge" method="post">
<input type="text" style="display: none;"
name="name" value="<%= element.name %>">
<button type="submit">Discharge</button>
</form>
For Discharging patients, we have to create a Discharge route where we are going to fetch the requested patient's name and search for the patient who has the same name, and delete the element.
app.post('/discharge', (req, res) => {
var name = req.body.name;
var j = 0;
patients.forEach(patient => {
j = j + 1;
if (patient.name == name) {
patients.splice((j - 1), 1)
}
})
res.render("home", {
data: patients
})
})
Example: Implementation to write the full code for app.js file to build HMS.
HTML
<!-- home.ejs -->
<!DOCTYPE html>
<html>
<head>
<title>HMS</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 Patients</h1>
<table>
<tr>
<th>Name</th>
<th>Number</th>
<th>DOB</th>
<th>City</th>
<th>Room No.</th>
<th>Discharge</th>
</tr>
<% data.forEach(element=> { %>
<tr>
<td><%= element.name %></td>
<td><%= element.number %></td>
<td><%= element.dob %></td>
<td><%= element.city %></td>
<td><%= element.roomNo %></td>
<td>
<form action="/discharge" method="post">
<input type="text" style="display: none;"
name="name" value="<%= element.name %>">
<button type="submit">Discharge</button>
</form>
</td>
</tr>
<% }) %>
</table>
<h1>Add Patient</h1>
<form action="/" method="post">
<input type="text" placeholder="Name" name="name">
<input type="number" placeholder="Number" name="number">
<input type="text" placeholder="DOB" name="dob">
<input type="text" placeholder="City" name="city">
<button type="submit">Add</button>
</form>
</body>
</html>
JavaScript
// app.js
const express = require('express')
const bodyParser = require('body-parser')
const patients = [{
name: 'Aditya',
number: '8175826846',
dob: '29/09/2001',
city: 'Mirzapur',
roomNo: '1',
}]
const availableBeds = 2;
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: patients
})
})
app.post("/", (req, res) => {
const name = req.body.name
const number = req.body.number
const dob = req.body.dob
const city = req.body.city
if (patients.length < availableBeds) {
const roomNo = patients.length + 1;
patients.push({
name: name,
number: number,
dob: dob,
city: city,
roomNo: roomNo
})
res.render("home", {
data: patients
})
}
else {
res.send("No room available");
}
})
app.post('/discharge', (req, res) => {
var name = req.body.name;
var j = 0;
patients.forEach(patient => {
j = j + 1;
if (patient.name == name) {
patients.splice((j - 1), 1)
}
})
res.render("home", {
data: patients
})
})
app.listen(3000, (req, res) => {
console.log("App is running on port 3000")
})
Steps to run the application: Inside the terminal type the command to run your script.
node app.js
Output:
Similar Reads
How to build Hostel Management System using Node.js ?
In this article, we are going to create a Hostel Management System. A Hostel Management System is used to manage the record of students of a college to which the college provides a hostel, where a college can view all the student data including their names, roll number, date of birth, city, phone nu
9 min read
How to Build Employee Management System using Node.js ?
An Employee Management System (EMS) is a crucial tool for businesses to efficiently manage their workforce. It allows companies to handle tasks like displaying employee details, adding new employees, removing existing ones, promoting employees, and updating salaries. In this article, we'll walk thro
8 min read
How to Build User Management System Using NodeJS?
A User Management System is an essential application for handling user accounts and information. It involves creating, reading, updating, and deleting user accounts, also known as CRUD operations. In this article, we will walk through how to build a simple User Management System using NodeJS. What W
6 min read
How to Build Library Management System Using NodeJS?
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. In this article, we will walk through how to build a simple Library Management System using NodeJS. What We
6 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
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
Hospital Management System using MEAN Stack
The Hospital Management App is an important application that ensures seamless coordination to revolutionize healthcare administration. In this article, we will be creating a Hospital Management Website using the MEAN stack â i.e. MongoDB, Express, Angular, and Node.js, with step by step process for
15+ min read
Hotel Booking System using Node.js and MongoDB
In the hotel booking system, there will be a user for him/her name, email, and room no. they will get after booking. for that, we have to make schema, and as well as we have two APIs. One API for getting data from the database and another API sending data to the database room no, name, email, and al
2 min read
How to build Love Calculator using Node.js ?
In this article, we are going to create a Love Calculator. A Love Calculator is used to calculate the love percentage between the partners. Functionality: Take User's NameTake User's Partner NameShow the Love PercentageApproach: We are going to use Body Parser by which we can capture user input valu
5 min read
How to Make to do List using Nodejs ?
Creating a to-do list application using Node.js involves setting up an Express server, creating RESTful APIs for CRUD operations, and using a database to store tasks. Enhance functionality with features like task prioritization and deadlines. Table of Content FeaturesHow the application worksSteps t
12 min read