Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dotenv.config({path:'./.env.local'})



app.use('/api/health',require('./routes/health-route'));
app.use('/api/fdata',require('./routes/fdata-route'));
app.use('/api/sdata',require('./routes/sdata-route'));

Expand Down
8 changes: 8 additions & 0 deletions backend/routes/health-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express=require('express')
const router=express.Router()

router.get('',(req,res)=>{
res.status(200).json({ status: 'ok' })
})

module.exports = router
Comment on lines +1 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

A robust health check should verify the status of critical dependencies like the database connection. Currently, the route returns 200 OK even if the database connection is lost or disconnected. Checking mongoose.connection.readyState ensures that orchestrators (like Kubernetes or AWS ECS) or load balancers can accurately detect when the service is unhealthy and take appropriate action (e.g., restarting the container or stopping traffic routing).

Suggested change
const express=require('express')
const router=express.Router()
router.get('',(req,res)=>{
res.status(200).json({ status: 'ok' })
})
module.exports = router
const express = require('express')
const { default: mongoose } = require('mongoose')
const router = express.Router()
router.get('', (req, res) => {
const dbState = mongoose.connection.readyState
if (dbState !== 1) {
return res.status(503).json({
status: 'error',
database: 'disconnected'
})
}
res.status(200).json({ status: 'ok' })
})
module.exports = router