feat: add /api/health endpoint for uptime monitoring

This commit is contained in:
Jennie Robinson Faber 2026-04-04 12:35:26 +01:00
parent 1875f16d48
commit 15fdf77be8

19
server/api/health.get.js Normal file
View file

@ -0,0 +1,19 @@
import mongoose from 'mongoose'
import { connectDB } from '../utils/mongoose.js'
export default defineEventHandler(async () => {
try {
await connectDB()
const dbState = mongoose.connection.readyState
// readyState: 0 = disconnected, 1 = connected, 2 = connecting, 3 = disconnecting
if (dbState !== 1) {
throw new Error('Database not connected')
}
return { status: 'ok', db: 'connected' }
} catch {
throw createError({
statusCode: 503,
data: { status: 'error', db: 'disconnected' },
})
}
})