health.js•3.84 kB
/**
* Health check routes
* Used by Railway for monitoring
*/
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const logger = require('../utils/logger');
/**
* @route GET /health
* @desc Health check endpoint
* @access Public
*/
router.get('/', async (req, res) => {
try {
// Check database connection
const dbStatus = mongoose.connection.readyState === 1 ? 'connected' : 'disconnected';
// Check memory usage
const memoryUsage = process.memoryUsage();
// Check uptime
const uptime = process.uptime();
// Return health status
return res.status(200).json({
status: 'success',
message: 'Service is healthy',
timestamp: new Date().toISOString(),
database: {
status: dbStatus
},
system: {
uptime: uptime,
memory: {
rss: Math.round(memoryUsage.rss / 1024 / 1024) + 'MB',
heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024) + 'MB',
heapUsed: Math.round(memoryUsage.heapUsed / 1024 / 1024) + 'MB'
},
node: process.version,
env: process.env.NODE_ENV
}
});
} catch (error) {
logger.error(`Health check failed: ${error.message}`);
return res.status(500).json({
status: 'error',
message: 'Service is unhealthy',
error: error.message
});
}
});
/**
* @route GET /health/db
* @desc Database health check endpoint
* @access Public
*/
router.get('/db', async (req, res) => {
try {
// Check database connection
if (mongoose.connection.readyState !== 1) {
throw new Error('Database connection is not established');
}
// Ping database
await mongoose.connection.db.admin().ping();
return res.status(200).json({
status: 'success',
message: 'Database connection is healthy',
timestamp: new Date().toISOString()
});
} catch (error) {
logger.error(`Database health check failed: ${error.message}`);
return res.status(500).json({
status: 'error',
message: 'Database connection is unhealthy',
error: error.message
});
}
});
/**
* @route GET /health/deep
* @desc Deep health check endpoint
* @access Public
*/
router.get('/deep', async (req, res) => {
try {
// Check database connection
if (mongoose.connection.readyState !== 1) {
throw new Error('Database connection is not established');
}
// Ping database
await mongoose.connection.db.admin().ping();
// Check memory usage
const memoryUsage = process.memoryUsage();
const heapUsedPercentage = (memoryUsage.heapUsed / memoryUsage.heapTotal) * 100;
// Check if memory usage is too high
if (heapUsedPercentage > 90) {
throw new Error('Memory usage is too high');
}
// Check uptime
const uptime = process.uptime();
return res.status(200).json({
status: 'success',
message: 'Service is healthy',
timestamp: new Date().toISOString(),
database: {
status: 'connected',
ping: 'successful'
},
system: {
uptime: uptime,
memory: {
rss: Math.round(memoryUsage.rss / 1024 / 1024) + 'MB',
heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024) + 'MB',
heapUsed: Math.round(memoryUsage.heapUsed / 1024 / 1024) + 'MB',
heapUsedPercentage: heapUsedPercentage.toFixed(2) + '%'
},
node: process.version,
env: process.env.NODE_ENV
}
});
} catch (error) {
logger.error(`Deep health check failed: ${error.message}`);
return res.status(500).json({
status: 'error',
message: 'Service is unhealthy',
error: error.message
});
}
});
module.exports = router;