Project Final
Project Final
js
server.js //App.js
import React from 'react';
// server.js import {
const express = require('express'); BrowserRouter as Router,
const mongoose = require('mongoose'); Routes, Route,
const cors = require('cors'); Link, useNavigate
const bodyParser = require('body-parser'); } from 'react-router-dom';
const patientsRouter = import Appointments
require('./routes/patients'); from './components/Appointments';
const doctorsRouter = import Doctors from './components/Doctors';
require('./routes/doctors'); import Patients from './components/Patients';
const appoinmentsRouter = import './App.css'
require('./routes/appointments')
const app = express(); const App = () => {
const PORT = process.env.PORT || 5000; const isLinkActive =
(path) =>
app.use(cors());
window.location.pathname === path;
app.use(bodyParser.json());
return (
<Router>
// Connect to MongoDB <div
mongoose.connect( className="container">
'mongodb://localhost:27017/hospital' <h1 style={{
, color: "green" }}>
{ GFG-
Hospital Managment App
useNewUrlParser: true,
</h1>
useUnifiedTopology: true <nav>
}); <ul>
const connection = mongoose.connection;
connection.once('open', () => { <li className={
console.log('MongoDB database isLinkActive('/appointments')
? 'active' : ''}>
connection established successfully');
<Link to="/appointments">
}); Appointments
</Link>
app.use('/patients', patientsRouter); </li>
app.use('/doctors', doctorsRouter);
app.use('/appointments', appoinmentsRouter) <li className={
isLinkActive('/doctors') ?
'active' : ''}>
app.listen(PORT, () => {
<Link to="/doctors">
console.log(`Server is running on port Doctors
${PORT}`); </Link>
}); </li>
<li className={
// models/Doctor.js
isLinkActive('/patients') ?
const mongoose = require('mongoose');
'active' : ''}> const Schema = mongoose.Schema;
const doctorSchema = new Schema({
<Link to="/patients"> name: { type: String, required: true },
Patients
specialty: { type: String, required: true
</Link>
</li> },
</ul> // Add more fields as needed
</nav> });
<Routes>
const Doctor =
<Route path="/appointments" mongoose.model('Doctor',
element={<Appointments />} />
doctorSchema);
<Route path="/"
element={<Appointments />} />
<Route path="/doctors" module.exports = Doctor;
element={<Doctors />} />
<Route path="/patients" // models/Patient.js
element={<Patients />} />
</Routes>
const mongoose = require('mongoose');
</div>
</Router> const Schema = mongoose.Schema;
); const patientSchema = new Schema({
} name: { type: String, required: true },
age: { type: Number, required: true },
export default App; gender: { type: String, required: true },
// Add more fields as needed
// models/Appointment.js });
const Patient = mongoose.model('Patient',
const mongoose = require('mongoose'); patientSchema);
const Schema = mongoose.Schema; module.exports = Patient;
const appointmentSchema = new Schema({
patientName: { type: String, required: // routes/appointments.js
true },
doctorName: { type: String, required: const express = require('express');
true }, const router = express.Router();
const Appointment =
date: { type: Date, required: true },
require('../models/Appointment');
// Add more fields as needed
}); // Get all appointments
router.route('/').get((req, res) => {
const Appointment = Appointment.find()
mongoose.model('Appointment', .then(appointments =>
appointmentSchema);
res.json(appointments))
module.exports = Appointment;
.catch(err =>
appointment.patientName =
req.body.patientName;
appointment.doctorName =
req.body.doctorName;
appointment.date =
req.body.date;
appointment.save()
.then(
() =>
res.json('Appointment updated!'))
.catch(
err =>
res.status(400)
// Delete appointment