EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
test.js
Go to the documentation of this file.
1/**
2 * @file Database Health Check API Endpoint
3 * @description
4 * Provides simple health check endpoint to verify database connectivity and API availability.
5 * Returns current database timestamp to confirm both database connection and query execution
6 * are functioning correctly. Useful for monitoring systems, load balancers, and uptime checks.
7 *
8 * Key features:
9 * - **No authentication**: Public endpoint for health monitoring
10 * - **Database connectivity test**: Executes simple SELECT NOW() query
11 * - **Fast response**: Minimal processing for sub-100ms response time
12 *
13 * Use cases:
14 * - Load balancer health checks (ALB, ELB, HAProxy)
15 * - Kubernetes liveness/readiness probes
16 * - Monitoring systems (Prometheus, Datadog, New Relic)
17 * - CI/CD pipeline validation
18 * - Manual diagnostic checks
19 * @module routes/test
20 * @requires express
21 * @requires ../services/db - PostgreSQL database pool
22 */
23
24const express = require('express');
25const router = express.Router();
26const pool = require('../services/db');
27
28/**
29 * @api {get} /test Database Health Check
30 * @apiName DatabaseHealthCheck
31 * @apiGroup Health
32 * @apiDescription
33 * Verifies database connectivity by executing simple SELECT NOW() query and returning
34 * current database timestamp. Used by monitoring systems and load balancers to determine
35 * if API and database are healthy.
36 * @apiPermission public
37 * @apiNote No authentication required - designed for health check systems
38 * @apiSuccess {string} time Current database timestamp (ISO 8601 format)
39 * @apiError (500) {String} error "Database connection failed" (plaintext, not JSON)
40 * @apiExample {curl} Example Request:
41 * curl https://api.ibghub.com/api/test
42 * @apiExample {json} Example Response:
43 * {
44 * "time": "2025-01-15T14:22:35.123Z"
45 * }
46 */
47
48router.get('/', async (req, res) => {
49 try {
50 const result = await pool.query('SELECT NOW()');
51 res.json({ time: result.rows[0].now });
52 } catch (err) {
53 console.error('DB connection error:', err);
54 res.status(500).send('Database connection failed');
55 }
56});
57
58module.exports = router;