Health check endpoints help you quickly see if your web app is running well. They give a simple answer to say "I am alive and working."
Health check endpoints in Express
app.get('/health', (req, res) => { res.status(200).send('OK'); });
This creates a GET route at '/health' that sends back a 200 status and 'OK' text.
Use simple responses so checks are fast and reliable.
app.get('/health', (req, res) => { res.status(200).send('OK'); });
app.get('/health', (req, res) => { res.json({ status: 'healthy' }); });
app.get('/health', (req, res) => { const uptime = process.uptime(); res.json({ status: 'healthy', uptime: uptime }); });
This Express app listens on port 3000 and has a health check endpoint at '/health'. When you visit '/health', it responds with 'OK' and status 200.
import express from 'express'; const app = express(); const PORT = 3000; app.get('/health', (req, res) => { res.status(200).send('OK'); }); app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });
Keep health check endpoints simple and fast to avoid slowing your app.
Use status code 200 to indicate healthy, other codes for problems.
Make sure health checks do not require authentication so monitoring tools can access them easily.
Health check endpoints tell if your app is running and ready.
They are simple GET routes that return a quick response like 'OK'.
Use them to help monitoring, load balancing, and debugging.
Practice
Solution
Step 1: Understand what a health check endpoint does
A health check endpoint is a simple route that tells if the app is alive and working.Step 2: Compare options to this purpose
Only To confirm the app is running and responsive matches this purpose. Other options are unrelated to health checks.Final Answer:
To confirm the app is running and responsive -> Option BQuick Check:
Health check = Confirm app running [OK]
- Thinking health check handles user data
- Confusing health check with static file serving
- Assuming it manages database connections
Solution
Step 1: Identify the HTTP method for health check
Health checks use GET requests to check status, so app.get is correct.Step 2: Check syntax correctness
app.get('/health', (req, res) => res.send('OK')); uses app.get with path '/health' and sends 'OK' response correctly.Final Answer:
app.get('/health', (req, res) => res.send('OK')); -> Option CQuick Check:
GET route with res.send('OK') = app.get('/health', (req, res) => res.send('OK')); [OK]
- Using POST instead of GET
- Using app.use instead of app.get
- Trying to listen on a route path
app.get('/health', (req, res) => {
res.status(200).send('Service is up');
});Solution
Step 1: Analyze the route handler
The route listens for GET on '/health' and sends status 200 with text 'Service is up'.Step 2: Determine the HTTP response
The client receives status 200 OK and the string 'Service is up' in the response body.Final Answer:
HTTP 200 OK with body 'Service is up' -> Option AQuick Check:
Status 200 + send text = HTTP 200 OK with body 'Service is up' [OK]
- Assuming 404 if route exists
- Confusing status codes
- Expecting no response or error
app.get('/health', (req, res) => {
res.sendStatus(200);
res.send('OK');
});Solution
Step 1: Understand res.sendStatus behavior
res.sendStatus(200) sends status and ends the response immediately.Step 2: Identify why res.send('OK') causes error
Calling res.send after response ended causes an error. So remove the second send.Final Answer:
Remove res.send('OK') because res.sendStatus ends response -> Option AQuick Check:
One response per request = Remove res.send('OK') because res.sendStatus ends response [OK]
- Sending multiple responses in one route
- Confusing res.sendStatus and res.status
- Adding next() unnecessarily
Solution
Step 1: Understand JSON response method
res.json sends a JSON object with correct headers automatically.Step 2: Check each option's response format
app.get('/health', (req, res) => { res.json({ status: 'ok', uptime: process.uptime() }); }); sends a JSON object with keys 'status' and 'uptime' correctly. app.get('/health', (req, res) => { res.send('status: ok, uptime: ' + process.uptime()); }); sends a string, not JSON. app.get('/health', (req, res) => { res.status(200).send({ status: 'ok', uptime: process.uptime() }); }); sends an object with res.send but may not set JSON headers properly. app.get('/health', (req, res) => { res.json('status: ok, uptime: ' + process.uptime()); }); sends a string inside res.json, which is incorrect.Final Answer:
app.get('/health', (req, res) => { res.json({ status: 'ok', uptime: process.uptime() }); }); -> Option DQuick Check:
Use res.json with object for JSON response [OK]
- Sending JSON as string instead of object
- Using res.send with object without headers
- Confusing res.json and res.send
Ready to prove it?
Pass the test and this pattern moves unknown
