Health check endpoints help you quickly see if your app is running well. They tell you if the server is alive and ready to handle requests.
Health check endpoints in Node.js
app.get('/health', (req, res) => { res.status(200).send('OK'); });
This example uses Express.js to create a simple GET endpoint at '/health'.
The endpoint returns a 200 status with a simple message to show the app is healthy.
app.get('/health', (req, res) => { res.status(200).send('OK'); });
app.get('/health', (req, res) => { const uptime = process.uptime(); res.json({ status: 'ok', uptime: uptime }); });
app.get('/health', (req, res) => { // Here you could check database or other services const dbConnected = true; if (dbConnected) { res.status(200).send('Healthy'); } else { res.status(503).send('Service Unavailable'); } });
This Node.js program uses Express to create a server with a health check endpoint at '/health'. When you visit this URL, it returns a JSON object showing the app status and how long it has been running.
import express from 'express'; const app = express(); const port = 3000; app.get('/health', (req, res) => { const uptime = process.uptime(); res.json({ status: 'ok', uptime: uptime.toFixed(2) }); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Keep health check endpoints simple and fast to avoid slowing down your app.
Use status codes: 200 means healthy, 503 means service unavailable.
Consider checking important dependencies like databases or APIs in your health check.
Health check endpoints show if your app is alive and ready.
They usually return a simple message or JSON with status info.
Use them to help monitor and manage your app in real time.
Practice
Solution
Step 1: Understand the role of health check endpoints
Health check endpoints are designed to verify if the application is alive and can respond to requests.Step 2: Differentiate from other endpoints
Endpoints for login, static files, or database connections serve different purposes unrelated to health checks.Final Answer:
To confirm the app is running and responsive -> Option AQuick Check:
Health check = confirms app is alive [OK]
- Confusing health check with user authentication
- Thinking health check serves static content
- Assuming health check manages database connections
Solution
Step 1: Identify the HTTP method for health checks
Health checks typically use GET requests to allow simple status queries.Step 2: Check Express syntax for route definition
Using app.get with a path and a callback sending a response is the correct pattern.Final Answer:
app.get('/health', (req, res) => res.send('OK')) -> Option CQuick Check:
GET route with res.send('OK') = correct syntax [OK]
- Using POST instead of GET for health checks
- Using app.use which matches all methods
- Confusing app.listen with route handling
/health?
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.json({ status: 'ok', uptime: process.uptime() });
});
app.listen(3000);Solution
Step 1: Analyze the route handler
The route '/health' uses res.json to send a JSON object with keys 'status' and 'uptime'.Step 2: Understand process.uptime()
process.uptime() returns the number of seconds the Node.js process has been running, a number value.Final Answer:
A JSON object with status 'ok' and uptime in seconds -> Option DQuick Check:
res.json sends JSON = JSON object response [OK]
- Thinking res.json is undefined
- Expecting plain text instead of JSON
- Assuming route is missing or broken
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.sendStatus(200).send('OK');
});
app.listen(3000);Solution
Step 1: Understand res.sendStatus behavior
res.sendStatus(200) sends the status and ends the response, so chaining .send('OK') causes an error.Step 2: Use correct method chaining
res.status(200).send('OK') sets status and sends body correctly without ending response prematurely.Final Answer:
Replace res.sendStatus(200).send('OK') with res.status(200).send('OK') -> Option AQuick Check:
res.status().send() chains properly, res.sendStatus() ends response [OK]
- Chaining send after sendStatus causes error
- Changing HTTP method unnecessarily
- Calling next() after sending response
status as 'ok' only if the database connection is alive, otherwise status is 'error'. Which approach correctly implements this in Node.js with Express?Solution
Step 1: Recognize async database check
Checking database connection is asynchronous, so await or then() must be used to get result.Step 2: Validate correct async usage
app.get('/health', async (req, res) => { const dbAlive = await checkDbConnection(); res.json({ status: dbAlive ? 'ok' : 'error' }); }); uses async/await properly to wait for checkDbConnection result before sending JSON response.Final Answer:
Use async function with await to check DB and respond accordingly -> Option BQuick Check:
Async/await needed for async DB check [OK]
- Calling async function without await or then
- Ignoring DB status and always returning 'ok'
- Not handling promise results properly
Ready to prove it?
Pass the test and this pattern moves unknown
