0% found this document useful (0 votes)
8 views4 pages

Hospital

The document outlines a simple healthcare management application using Node.js and Express. It includes a server that manages patient data stored in a JSON file, allowing users to add new patients and retrieve a list of existing ones. The frontend is built with HTML and JavaScript, providing an interface for user interaction to manage patient information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Hospital

The document outlines a simple healthcare management application using Node.js and Express. It includes a server that manages patient data stored in a JSON file, allowing users to add new patients and retrieve a list of existing ones. The frontend is built with HTML and JavaScript, providing an interface for user interaction to manage patient information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.save it as patients.

json
[]
2.save it as app.js
const express = require('express');
const fs = require('fs');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');

const app = express();


const PORT = 3000;
const FILE = './patients.json';

app.use(cors());
app.use(bodyParser.json());
app.use(express.static(__dirname)); // Serve static files like index.html

// Get all patients


app.get('/patients', (req, res) => {
const data = JSON.parse(fs.readFileSync(FILE));
res.json(data);
});

// Add a new patient


app.post('/patients', (req, res) => {
const data = JSON.parse(fs.readFileSync(FILE));
const patient = req.body;
data.push(patient);
fs.writeFileSync(FILE, JSON.stringify(data, null, 2));
res.send('Patient added successfully');
});

app.listen(PORT, () => {
console.log(`App running at http://localhost:${PORT}`);
});
3.save it as index.html
<!DOCTYPE html>
<html>
<head>
<title>Healthcare Management</title>
<style>
body { font-family: Arial; margin: 40px; }
input, button { margin: 5px; padding: 8px; }
</style>
</head>
<body>
<h1>Healthcare Patient Management</h1>

<input id="name" placeholder="Patient Name" />


<input id="age" placeholder="Age" type="number" />
<input id="condition" placeholder="Medical Condition" />
<button onclick="addPatient()">Add Patient</button>
<h2>Patient List</h2>
<ul id="patientList"></ul>

<script>
async function addPatient() {
const name = document.getElementById('name').value.trim();
const age = document.getElementById('age').value.trim();
const condition = document.getElementById('condition').value.trim();
if (!name || !age || !condition) {
alert('Please fill all fields');
return;
}
await fetch('/patients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, age, condition })
});
document.getElementById('name').value = '';
document.getElementById('age').value = '';
document.getElementById('condition').value = '';
loadPatients();
}

async function loadPatients() {


const res = await fetch('/patients');
const data = await res.json();
const list = document.getElementById('patientList');
list.innerHTML = '';
data.forEach(p => {
const li = document.createElement('li');
li.textContent = `${p.name} (Age: ${p.age}) - Condition: ${p.condition}`;
list.appendChild(li);
});
}

loadPatients();
</script>
</body>
</html>

Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy