,

Develop a Backend for a Hospital Management System using MERN Stack – React, MongoDB, Express, and NodeJs (Part 2)

Posted by

Build a MERN Stack Hospital Management System Part 2

Building a MERN Stack Hospital Management System – Part 2 (Backend)

Setting up Express and NodeJs

First, let’s set up our backend using Express for creating the server and handling API requests and NodeJs for running our server. Make sure you have NodeJs installed on your machine.


npm install express mongoose
npm install nodemon

Create a new file called server.js and add the following code:


const express = require('express');
const mongoose = require('mongoose');

const app = express();

// Connect to MongoDB
mongoose.connect('mongodb://localhost/hospital-db', {
useNewUrlParser: true,
useUnifiedTopology: true
});

app.listen(5000, () => {
console.log('Server is running on port 5000');
});

Now, you can run the server using nodemon server.js and you should see the message ‘Server is running on port 5000’ in your terminal.

Creating Models and Routes

Next, let’s create some models and routes for our Hospital Management System. Create a new folder called models and add a file called patient.js with the following code:


const mongoose = require('mongoose');

const patientSchema = new mongoose.Schema({
name: String,
age: Number,
gender: String,
medicalHistory: String
});

const Patient = mongoose.model('Patient', patientSchema);

module.exports = Patient;

Now, create a new folder called routes and add a file called patients.js with the following code:


const express = require('express');
const Patient = require('../models/patient');

const router = express.Router();

router.get('/', async (req, res) => {
try {
const patients = await Patient.find();
res.json(patients);
} catch (err) {
res.status(500).send(err);
}
});

module.exports = router;

Finally, in our server.js file, add the following code to connect our routes:


const patientRoutes = require('./routes/patients');

app.use('/patients', patientRoutes);

Now you can test your backend by making a GET request to http://localhost:5000/patients and you should see an empty array.

That’s it for Part 2 of building a MERN Stack Hospital Management System. Stay tuned for Part 3 where we will build the frontend in React.

0 0 votes
Article Rating
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@simont733
5 months ago

I wish it has QR payments option which is when a customer required to pay the service also PDF E-voucher thats send to your email, or can you make Part 3 add pharmacy Tab inside your project so when the doctor order medicine its goes to pharmacy page and the payments option will be available

@Hari-si6lc
5 months ago

Awesome Video, some assets are missing along with the admin details. Kindly help

@evlads78
5 months ago

.gitignore for the BE?

@vipulgupta4125
5 months ago

need more Projects like this.

@SonnyTechAcademy
5 months ago

Keep going man

@JimohSegun
5 months ago

There's no timestamps in your videos