Performance: Health check endpoints
Health check endpoints affect server responsiveness and uptime monitoring, impacting how quickly a system can report its status without slowing down main request handling.
app.get('/health', (req, res) => { // Simple quick check res.json({ status: 'ok' }); });
app.get('/health', (req, res) => { // Heavy synchronous CPU operation let sum = 0; for(let i = 0; i < 1e8; i++) { sum += i; } res.json({ status: 'ok', data: sum }); });
| Pattern | CPU Usage | Event Loop Blocking | Response Time | Verdict |
|---|---|---|---|---|
| Heavy sync CPU operation on health check | High | Yes, blocks event loop | 100+ ms | [X] Bad |
| Simple static JSON response | Minimal | No | <1 ms | [OK] Good |
/health?
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.json({ status: 'ok', uptime: process.uptime() });
});
app.listen(3000);const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.sendStatus(200).send('OK');
});
app.listen(3000);status as 'ok' only if the database connection is alive, otherwise status is 'error'. Which approach correctly implements this in Node.js with Express?Pass the test and this pattern moves unknown