Complete the code to create a basic health check endpoint using Express.
const express = require('express'); const app = express(); app.get('/health', (req, res) => { res.status([1]).send('OK'); }); app.listen(3000);
The health check endpoint should respond with HTTP status 200 to indicate the service is healthy.
Complete the code to send a JSON response with status and uptime in the health check endpoint.
app.get('/health', (req, res) => { res.status(200).json({ status: 'ok', uptime: process.[1]() }); });
memoryUsage or cpuUsage which return different info.The process.uptime() method returns the number of seconds the current Node.js process has been running, useful for health checks.
Fix the error in the health check middleware to properly handle asynchronous checks.
app.get('/health', async (req, res) => { const dbHealthy = await checkDatabaseConnection(); if (!dbHealthy) { res.status([1]).json({ status: 'error' }); } else { res.status(200).json({ status: 'ok' }); } });
When a dependent service like a database is down, the health check should respond with HTTP 503 Service Unavailable.
Fill both blanks to create a reusable health check middleware that sends JSON status and sets correct HTTP status code.
function healthCheckMiddleware(req, res) {
const healthy = checkSystemHealth();
res.status([1]).json({ status: [2] });
}The middleware should respond with status 200 and JSON status 'ok' when the system is healthy.
Fill all three blanks to create an advanced health check endpoint that checks database, cache, and returns appropriate status.
app.get('/health', async (req, res) => { const dbOk = await checkDB(); const cacheOk = await checkCache(); if (dbOk && cacheOk) { res.status([1]).json({ status: [2] }); } else { res.status([3]).json({ status: 'error' }); } });
When both database and cache are healthy, respond with 200 and status 'ok'. Otherwise, respond with 503 and status 'error'.