Hospital_Management_System_Code
Hospital_Management_System_Code
Frontend - App.js
import React, { useEffect, useState } from "react";
import { BrowserRouter as Router, Route, Routes, Navigate } from "react-router-dom";
import { Toaster } from "react-hot-toast";
import Navbar from "./components/Navbar";
import ProtectedRoute from "./components/ProtectedRoute";
import ThemeToggle from "./components/ThemeToggle";
import Notifications from "./components/Notifications";
import "./styles/globals.css";
function App() {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [theme, setTheme] = useState(localStorage.getItem("theme") || "light");
useEffect(() => {
const token = localStorage.getItem("token");
if (token) {
setIsAuthenticated(true);
}
}, []);
useEffect(() => {
document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
}, [theme]);
return (
<Router>
<Toaster position="top-right" />
{isAuthenticated && <Navbar />}
{isAuthenticated && <Notifications />}
{isAuthenticated && <ThemeToggle theme={theme} setTheme={setTheme} />}
<Routes>
<Route path="/login" element={<Login setAuth={setIsAuthenticated} />} />
<Route element={<ProtectedRoute isAuthenticated={isAuthenticated} />}>
<Route path="/" element={<Dashboard />} />
<Route path="/patients" element={<Patients />} />
<Route path="/doctors" element={<Doctors />} />
<Route path="/appointments" element={<Appointments />} />
<Route path="/billing" element={<Billing />} />
<Route path="/profile" element={<Profile />} />
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
</Router>
);
}
Backend - Server.js
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/hospitalDB", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use("/api/users", userRoutes);
app.use("/api/patients", patientRoutes);
app.use("/api/doctors", doctorRoutes);
app.use("/api/appointments", appointmentRoutes);