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.
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
Awesome Video, some assets are missing along with the admin details. Kindly help
.gitignore for the BE?
need more Projects like this.
Keep going man
There's no timestamps in your videos