Approach to create backend
- Create MongoDB Models:
- Design MongoDB models for `Appointment`, `Doctor`, and `Patient` in separate files (`Appointment.js`, `Doctor.js`, `Patient.js`).
- Set Up Express Routes:
- Create separate routes for appointments, doctors, and patients (`appointments.js`, `doctors.js`, `patients.js`).
- Implement CRUD (Create, Read, Update, Delete) operations for each resource.
- Connect to MongoDB:
- In your main `server.js`, connect to MongoDB using Mongoose.
- Don’t forget to handle connection errors.
Example: Below is the code for the all the above files named above:
Javascript
// server.js const express = require( 'express' ); const mongoose = require( 'mongoose' ); const cors = require( 'cors' ); const bodyParser = require( 'body-parser' ); const patientsRouter = require( './routes/patients' ); const doctorsRouter = require( './routes/doctors' ); const appoinmentsRouter = require( './routes/appointments' ) const app = express(); const PORT = process.env.PORT || 5000; app.use(cors()); app.use(bodyParser.json()); // Connect to MongoDB mongoose.connect( 'mongodb://localhost:27017/hospital' , { useNewUrlParser: true , useUnifiedTopology: true }); const connection = mongoose.connection; connection.once( 'open' , () => { console.log( 'MongoDB database connection established successfully' ); }); app.use( '/patients' , patientsRouter); app.use( '/doctors' , doctorsRouter); app.use( '/appointments' , appoinmentsRouter) app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); }); |
Javascript
// routes/appointments.js const express = require( 'express' ); const router = express.Router(); const Appointment = require( '../models/Appointment' ); // Get all appointments router.route( '/' ).get((req, res) => { Appointment.find() .then(appointments => res.json(appointments)) . catch (err => res.status(400).json( 'Error: ' + err)); }); // Add new appointment router.route( '/add' ).post((req, res) => { const { patientName, doctorName, date } = req.body; const newAppointment = new Appointment({ patientName, doctorName, date }); newAppointment.save() .then(savedAppointment => res.json(savedAppointment)) . catch (err => res.status(400).json( 'Error: ' + err)); }); // Update appointment data router.route( '/update/:id' ).post((req, res) => { Appointment.findById(req.params.id) .then(appointment => { 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) .json( 'Error: ' + err)); }) . catch ( err => res.status(400) .json( 'Error: ' + err)); }); // Delete appointment router.route( '/delete/:id' ) . delete ((req, res) => { Appointment.findByIdAndDelete(req.params.id) .then( () => res .json( 'Appointment deleted.' )) . catch ( err => res .status(400).json( 'Error: ' + err)); }); module.exports = router; |
Javascript
// routes/doctors.js const express = require( 'express' ); const router = express.Router(); const Doctor = require( '../models/Doctor' ); // Get all doctors router.route( '/' ).get((req, res) => { Doctor.find() .then(doctors => res.json(doctors)) . catch (err => res.status(400) .json( 'Error: ' + err)); }); // Add new doctor router.route( '/add' ) .post((req, res) => { const { name, specialty } = req.body; const newDoctor = new Doctor({ name, specialty }); newDoctor.save() // Return the savedDoctor object .then(savedDoctor => res.json(savedDoctor)) . catch ( err => res.status(400) .json( 'Error: ' + err)); }); // Update doctor data router.route( '/update/:id' ) .post((req, res) => { Doctor.findById(req.params.id) .then(doctor => { if (!doctor) { return res.status(404) .json( 'Doctor not found' ); } doctor.name = req.body.name; doctor.specialty = req.body.specialty; doctor.save() .then(() => res.json( 'Doctor updated!' )) . catch (err => res.status(400) .json( 'Error: ' + err)); }) . catch (err => res.status(400) .json( 'Error: ' + err)); }); // Delete doctor by ID router.route( '/delete/:id' ). delete ((req, res) => { Doctor.findByIdAndDelete(req.params.id) .then(doctor => { if (!doctor) { return res.status(404) .json( 'Doctor not found' ); } res.json( 'Doctor deleted!' ); }) . catch (err => res.status(400) .json( 'Error: ' + err)); }); module.exports = router; |
Javascript
// routes/patients.js const express = require( 'express' ); const router = express.Router(); const Patient = require( '../models/Patient' ); // Get all patients router.route( '/' ).get((req, res) => { Patient.find() .then(patients => res.json(patients)) . catch (err => res.status(400) .json( 'Error: ' + err)); }); // Add new patient router.route( '/add' ) .post((req, res) => { const { name, age, gender } = req.body; const newPatient = new Patient({ name, age, gender }); newPatient.save() .then(savedPatient => res.json(savedPatient)) . catch (err => res.status(400) .json( 'Error: ' + err)); }); // Update patient data router.route( '/update/:id' ) .post((req, res) => { console.log( 'hihhhhiuhiihihiuhiuh' ); Patient.findById(req.params.id) .then(patient => { if (!patient) { return res.status(404) .json( 'Patient not found' ); } patient.name = req.body.name; patient.age = req.body.age; patient.gender = req.body.gender; patient.save() .then(() => res.json( 'Patient updated!' )) . catch (err => res.status(400) .json( 'Error: ' + err)); }) . catch (err => res.status(400) .json( 'Error: ' + err)); }); // Delete patient by ID router.route( '/delete/:id' ) . delete ((req, res) => { Patient.findByIdAndDelete(req.params.id) .then(patient => { if (!patient) { return res.status(404) .json( 'Patient not found' ); } res.json( 'Patient deleted!' ); }) . catch (err => res.status(400) .json( 'Error: ' + err)); }); module.exports = router; |
Javascript
// models/Appointment.js const mongoose = require( 'mongoose' ); const Schema = mongoose.Schema; const appointmentSchema = new Schema({ patientName: { type: String, required: true }, doctorName: { type: String, required: true }, date: { type: Date, required: true }, // Add more fields as needed }); const Appointment = mongoose.model( 'Appointment' , appointmentSchema); module.exports = Appointment; |
Javascript
// models/Doctor.js const mongoose = require( 'mongoose' ); const Schema = mongoose.Schema; const doctorSchema = new Schema({ name: { type: String, required: true }, specialty: { type: String, required: true }, // Add more fields as needed }); const Doctor = mongoose.model( 'Doctor' , doctorSchema); module.exports = Doctor; |
Javascript
// models/Patient.js const mongoose = require( 'mongoose' ); const Schema = mongoose.Schema; const patientSchema = new Schema({ name: { type: String, required: true }, age: { type: Number, required: true }, gender: { type: String, required: true }, // Add more fields as needed }); const Patient = mongoose.model( 'Patient' , patientSchema); module.exports = Patient; |
Hospital Management Application using MERN Stack
In the fast-paced world of healthcare, it’s vital to manage hospital tasks effectively to ensure top-notch patient care. This article explores creating a strong Hospital Management App using the MERN stack – that’s MongoDB, Express, React, and Node.js, breaking down the process for easier understanding.
Preview of final output: Let us have a look at how the final application will look like.
Contact Us