Steps to Setup Backend with Node and Express
Step 1: Creating an express app
npm init -y
Step 2: Installing the required packages
npm install express mongoose
Folder Structure(Backend)
Dependencies
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2",
"mongoose": "^8.3.0"
}
Step 3: Below is the code for the backend.
// server.js
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const patientRoutes = require("./routes/patientRoutes");
const doctorRoutes = require("./routes/doctorRoutes");
const appointmentRoutes = require("./routes/appointmentRoutes");
const app = express();
const PORT = process.env.PORT || 4000;
// Middleware
app.use(cors());
app.use(express.json());
MONGO_URI = "YOUR_MONGO_URI";
// Connect to MongoDB
mongoose.connect(MONGO_URI).then(() => {
console.log("Connected to MongoDB!");
});
// Routes
app.use("/api/patient", patientRoutes);
app.use("/api/doctor", doctorRoutes);
app.use("/api/appointment", appointmentRoutes);
// Start server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// models/Appointment.js
const mongoose = require("mongoose");
const appointmentSchema = new mongoose.Schema({
patient: { type: String, required: true },
doctor: { type: String, required: true },
appointmentDate: { type: Date, required: true },
});
const Appointment = mongoose.model("Appointment", appointmentSchema);
module.exports = Appointment;
// models/Doctor.js
const mongoose = require("mongoose");
const doctorSchema = new mongoose.Schema({
name: { type: String, required: true },
speciality: { type: String, required: true },
});
const Doctor = mongoose.model("Doctor", doctorSchema);
module.exports = Doctor;
// models/Patient.js
const mongoose = require("mongoose");
const patientSchema = new mongoose.Schema({
name: { type: String, required: true },
age: { type: String, required: true },
gender: { type: String, required: true },
});
const Patient = mongoose.model("Patient", patientSchema);
module.exports = Patient;
// routes/appointmentRoutes.js
const express = require("express");
const router = express.Router();
const Appointment = require("../models/Appointment");
// Get all appointments
router.get("/get", async (req, res) => {
try {
const appointments = await Appointment.find();
res.status(200).json(appointments);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Add a new appointment
router.post("/add", async (req, res) => {
console.log(req.body);
const appointment = new Appointment({
patient: req.body.patient,
doctor: req.body.doctor,
appointmentDate: req.body.appointmentDate,
});
try {
const newAppointment = await appointment.save();
console.log(newAppointment);
res.status(201).json(newAppointment);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Delete an appointment by ID
router.delete("/delete/:id", async (req, res) => {
try {
await Appointment.findByIdAndDelete(req.params.id);
res.json({ message: "Appointment deleted" });
} catch (error) {
console.error("Error deleting appointment:", error);
res.status(500).json({ message: error.message });
}
});
// Update a appointment by ID
router.put("/update/:id", async (req, res) => {
const appointmentId = req.params.id;
const { patient, doctor, appointmentDate } = req.body;
try {
// Find the appointment by ID in the database
const appointment = await Appointment
.findById(appointmentId);
if (!appointment) {
return res.status(404)
.json({ message: "Appointment not found" });
}
// Update the appointment properties
appointment.patient = patient;
appointment.doctor = doctor;
appointment.appointmentDate = appointmentDate;
// Save the updated appointment
await appointment.save();
console.log(appointment);
// You can send the updated appointment details in the response
res.json(appointment);
} catch (error) {
console.error("Error updating appointment details:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
module.exports = router;
// routes/doctorRoutes.js
const express = require("express");
const router = express.Router();
const Doctor = require("../models/Doctor");
// Get all doctors
router.get("/get", async (req, res) => {
try {
const doctors = await Doctor.find();
res.status(200).json(doctors);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Add a new doctor
router.post("/add", async (req, res) => {
const doctor = new Doctor({
name: req.body.name,
speciality: req.body.speciality,
});
try {
const newDoctor = await doctor.save();
console.log(newDoctor);
res.status(201).json(newDoctor);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Delete a doctor by ID
router.delete("/delete/:id", async (req, res) => {
try {
await Doctor.findByIdAndDelete(req.params.id);
res.json({ message: "Doctor deleted" });
} catch (error) {
console.error("Error deleting doctor:", error);
res.status(500).json({ message: error.message });
}
});
// Update a doctor by ID
router.put("/update/:id", async (req, res) => {
const doctorId = req.params.id;
const { name, speciality } = req.body;
try {
// Find the doctor by ID in the database
const doctor = await Doctor.findById(doctorId);
if (!doctor) {
return res.status(404).json({ message: "Doctor not found" });
}
// Update the doctor properties
doctor.name = name;
doctor.speciality = speciality;
// Save the updated doctor
await doctor.save();
console.log(doctor);
// You can send the updated doctor details in the response
res.json(doctor);
} catch (error) {
console.error("Error updating doctor details:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
module.exports = router;
// routes/patientRoutes.js
const express = require("express");
const router = express.Router();
const Patient = require("../models/Patient");
// Get all patients
router.get("/get", async (req, res) => {
try {
const patients = await Patient.find();
res.status(200).json(patients);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Add a new patient
router.post("/add", async (req, res) => {
const patient = new Patient({
name: req.body.name,
age: req.body.age,
gender: req.body.gender,
});
try {
const newPatient = await patient.save();
console.log(newPatient);
res.status(201).json(newPatient);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Delete a patient by ID
router.delete("/delete/:id", async (req, res) => {
try {
await Patient.findByIdAndDelete(req.params.id);
res.json({ message: "Patient deleted" });
} catch (error) {
console.error("Error deleting patient:", error);
res.status(500).json({ message: error.message });
}
});
// Update a patient by ID
router.put("/update/:id", async (req, res) => {
const patientId = req.params.id;
const { name, age, gender } = req.body;
try {
// Find the patient by ID in the database
const patient = await Patient.findById(patientId);
if (!patient) {
return res.status(404).json({ message: "Patient not found" });
}
// Update the patient properties
patient.name = name;
patient.age = age;
patient.gender = gender;
// Save the updated patient
await patient.save();
console.log(patient);
// You can send the updated patient details in the response
res.json(patient);
} catch (error) {
console.error("Error updating patient details:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
module.exports = router;
Step 4: Start the backend server
node server.js
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 easy understanding.
Project Preview: