0
0
Node.jsframework~10 mins

Health check endpoints in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Health check endpoints
Start Server
Receive HTTP Request
Check if URL is /health
Run Health Check
Return Status 200 and JSON
End Request
The server listens for requests, checks if the URL is /health, runs a health check, and returns a status response.
Execution Sample
Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  if (req.url === '/health') {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify({status: 'ok'}));
  } else {
    res.writeHead(404);
    res.end();
  }
});

server.listen(3000);
This code creates a simple HTTP server that responds with a JSON status when /health is requested.
Execution Table
StepRequest URLCondition (req.url === '/health')ActionResponse StatusResponse Body
1/healthtrueSend 200 with JSON {status: 'ok'}200{"status":"ok"}
2/otherfalseSend 404 with empty body404
💡 Requests not matching /health get 404; /health requests return 200 with status JSON.
Variable Tracker
VariableStartAfter Step 1After Step 2
req.urlundefined/health/other
res.statusCodeundefined200404
res.bodyundefined{"status":"ok"}
Key Moments - 2 Insights
Why does the server return 404 for URLs other than /health?
Because the condition req.url === '/health' is false for other URLs, so the else branch sends a 404 response as shown in execution_table row 2.
What does the server send back when the URL is /health?
It sends a 200 status with a JSON body {"status":"ok"}, as shown in execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status when the request URL is '/health'?
A500
B404
C200
D301
💡 Hint
Check the Response Status column in execution_table row 1.
At which step does the condition req.url === '/health' evaluate to false?
AStep 2
BStep 1
CBoth steps
DNever
💡 Hint
Look at the Condition column in execution_table row 2.
If the server code is changed to respond with status 503 on /health, how would the response status change in the table?
AIt would stay 200
BIt would change to 503
CIt would become 404
DIt would be empty
💡 Hint
Focus on the Action and Response Status columns for /health requests.
Concept Snapshot
Health check endpoints listen for a specific URL (e.g., /health).
If the request URL matches, respond with status 200 and a JSON status.
If not, respond with 404 or other error.
This helps external systems check if the server is running.
Use simple HTTP server code to implement.
Always return JSON for easy parsing.
Full Transcript
A health check endpoint is a special URL on a server that tells if the server is working. When the server gets a request, it checks if the URL is '/health'. If yes, it sends back a 200 status code and a JSON message {"status":"ok"}. If the URL is different, it sends a 404 status code. This lets other systems or users know if the server is alive and healthy. The example code uses Node.js's http module to create this simple server. The execution table shows two cases: one where the URL is '/health' and one where it is not. Variables like req.url and res.statusCode change accordingly. This pattern is common in web services to monitor uptime and readiness.