Skip to content

Bug: Duplicate /health Route in server.js — Real Health Check Is Dead Code, Always Returns ok #222

Description

@devprashant19

Affected File

Node.js Gatewaybackend/server.js


Bug Description

There are two GET /health route handlers registered in server.js. Express resolves routes in registration order and stops at the first match, so the second handler is never executed.

First handler (line 47 — always wins, returns a dumb static response):

app.get('/health', (req, res) => {
    res.json({ 
        status: 'ok', 
        message: 'Server is running',
        limit: '1MB'
    });
});

Second handler (line 104 — unreachable dead code, contains the real health check):

app.get("/health", async (req, res) => {
  try {
    const healthStatus = await getHealthStatus();
    const statusCode = healthStatus.status === "healthy" ? 200 : 503;
    res.status(statusCode).json(healthStatus);
  } catch (error) {
    res.status(500).json({ ... });
  }
});

getHealthStatus() (from utils/healthCheck.js) checks MongoDB connectivity and returns a 503 when the database is unavailable. Because this handler is never reached, the /health endpoint always returns 200 with {status:"ok"} — even when MongoDB is down and the server cannot process any requests.

Impact:

  • Docker and Kubernetes health probes see a healthy service even when MongoDB is disconnected, causing traffic to be routed to a broken instance.
  • Monitoring systems get false-positive health signals, hiding outages.
  • The getHealthStatus utility and all the infrastructure built around real health checking is wasted code.

Steps to Reproduce

  1. Start the Node server with MongoDB disconnected (e.g., wrong MONGODB_URI).
  2. Call GET http://localhost:3000/health.
  3. Observe response: {"status":"ok","message":"Server is running","limit":"1MB"} with HTTP 200.
  4. Expected: HTTP 503 with a degraded status indicating DB failure.

Expected Behavior

GET /health calls getHealthStatus(), returns 200 when all dependencies are healthy, and 503 when MongoDB is disconnected or any dependency fails.


Actual Behavior

GET /health always returns HTTP 200 {"status":"ok"} regardless of the actual service health.


Proposed Fix

Remove the first (dumb) handler at line 47 entirely. Keep only the second handler at line 104:

// DELETE lines 47–53 (the dumb static handler)

// Keep only this handler (line 104):
app.get("/health", async (req, res) => {
  try {
    const healthStatus = await getHealthStatus();
    const statusCode = healthStatus.status === "healthy" ? 200 : 503;
    res.status(statusCode).json(healthStatus);
  } catch (error) {
    res.status(500).json({
      status: 'error',
      message: 'Failed to retrieve health status',
      error: error.message
    });
  }
});

Scope of Contribution

  • Frontend
  • Backend
  • Database
  • API
  • Authentication
  • AI/ML
  • DevOps / CI-CD
  • Documentation
  • UI/UX

Pre-submission Checklist

  • I have checked existing issues for duplicates.
  • I have verified this issue exists in the current codebase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions