0
0
Expressframework~10 mins

Health check endpoints in Express - 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
Send 200 OK
End Request
The server starts and listens. When a request comes, it checks if the URL is '/health'. If yes, it sends a 200 OK response. Otherwise, it passes the request to other routes.
Execution Sample
Express
const express = require('express');
const app = express();
app.get('/health', (req, res) => res.send('OK'));
app.listen(3000);
This code creates a simple Express server with a health check endpoint at '/health' that responds with 'OK'.
Execution Table
StepIncoming Request URLCondition (/health?)ActionResponse Sent
1/healthYesSend 200 OK with 'OK'OK
2/api/dataNoPass to other routesNo response here
3/healthYesSend 200 OK with 'OK'OK
4/unknownNoPass to other routesNo response here
💡 Requests not matching '/health' are passed to other routes or middleware.
Variable Tracker
VariableStartRequest 1Request 2Request 3Request 4
req.urlN/A/health/api/data/health/unknown
res.statusCodeN/A200Not set200Not set
res.bodyN/A'OK'N/A'OK'N/A
Key Moments - 2 Insights
Why does the server respond only to '/health' and not other URLs?
Because the route handler is defined only for '/health' (see execution_table rows 1 and 2). Other URLs are passed to other routes or middleware.
What happens if a request URL is '/health' but the method is POST?
The handler is defined for GET requests only, so POST requests to '/health' will not match and will be passed on (similar to execution_table row 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response is sent when the request URL is '/health'?
A'OK' with status 200
BNo response
C404 Not Found
D'Error' message
💡 Hint
Check rows 1 and 3 in the execution_table where URL is '/health'.
At which request does the condition '/health?' evaluate to No?
ARequest 3
BRequest 1
CRequest 2
DRequest 4
💡 Hint
Look at the 'Condition (/health?)' column in execution_table.
If we add a POST handler for '/health', how would the response change for a POST request to '/health'?
AIt would cause a server error
BIt would respond with 200 OK for POST requests too
CIt would still pass to other routes
DIt would respond with 404 Not Found
💡 Hint
Think about how route handlers match method and path in Express.
Concept Snapshot
Health check endpoints in Express:
- Use app.get('/health', handler) to define a GET endpoint.
- When a request matches '/health', send a 200 OK response.
- Other URLs pass to other routes or middleware.
- Useful for monitoring server status simply.
- Keep response lightweight like 'OK'.
Full Transcript
This visual execution shows how an Express server handles health check endpoints. The server listens for HTTP requests. When a request comes in, it checks if the URL path is '/health'. If yes, it sends a 200 OK response with body 'OK'. If not, it passes the request to other routes or middleware. The execution table traces requests with different URLs and shows which get a response. Variables like request URL and response status are tracked. Key moments clarify why only '/health' matches and what happens with other methods. The quiz tests understanding of response behavior and routing. The snapshot summarizes how to set up a health check endpoint in Express simply and effectively.