Health checks help us know if our app is working well. Monitoring keeps an eye on the app to catch problems early.
Health checks and monitoring in Deno
import { serve } from "https://deno.land/std@0.203.0/http/server.ts"; serve((req) => { if (req.url.endsWith("/health")) { return new Response("OK", { status: 200 }); } return new Response("Not Found", { status: 404 }); });
This example shows a simple HTTP health check endpoint.
The server responds with status 200 and message "OK" when the /health path is requested.
import { serve } from "https://deno.land/std@0.203.0/http/server.ts"; serve((req) => { if (req.url.endsWith("/health")) { return new Response("OK", { status: 200 }); } return new Response("Not Found", { status: 404 }); });
import { serve } from "https://deno.land/std@0.203.0/http/server.ts"; serve((req) => { if (req.url.endsWith("/health")) { const healthInfo = { status: "healthy", uptime: Deno.metrics().opsCompleted }; return new Response(JSON.stringify(healthInfo), { status: 200, headers: { "Content-Type": "application/json" }, }); } return new Response("Not Found", { status: 404 }); });
This program starts a server that listens for HTTP requests. When you visit /health, it replies with "OK" and status 200, meaning the app is healthy. Other paths return 404 Not Found.
import { serve } from "https://deno.land/std@0.203.0/http/server.ts"; serve((req) => { if (req.url.endsWith("/health")) { return new Response("OK", { status: 200 }); } return new Response("Not Found", { status: 404 }); });
Health check endpoints should be simple and fast to respond.
Use monitoring tools to regularly call the health check URL and alert if it fails.
Make sure health checks do not expose sensitive information.
Health checks tell if your app is running well.
Monitoring watches your app and alerts you to problems.
In Deno, you can create a simple HTTP endpoint that returns status 200 for health checks.
Practice
Solution
Step 1: Understand health check concept
A health check endpoint is designed to confirm that the app is alive and responding to requests.Step 2: Identify the correct purpose
Options B, C, and D describe unrelated tasks like deployment, resource usage, or compilation, which are not health checks.Final Answer:
To verify if the application is running and responsive -> Option AQuick Check:
Health check = Verify app responsiveness [OK]
- Confusing health checks with deployment steps
- Thinking health checks improve performance directly
- Mixing health checks with code compilation
Solution
Step 1: Check correct import and usage
import { serve } from "https://deno.land/std/http/server.ts"; serve(req => new Response("OK"), { port: 8000 }); correctly imports 'serve' and returns a Response with "OK" for each request, which is a valid health check endpoint.Step 2: Identify incorrect usage in other options
import { listenAndServe } from "https://deno.land/std/http/server.ts"; listenAndServe(8000, () => console.log("OK")); uses deprecated 'listenAndServe' and does not respond to requests properly. Options A and C use console.log instead of returning a Response, so they won't respond correctly.Final Answer:
import { serve } from "https://deno.land/std/http/server.ts"; serve(req => new Response("OK"), { port: 8000 }); -> Option DQuick Check:
serve + Response("OK") = Correct health endpoint [OK]
- Using console.log instead of returning Response
- Using deprecated functions like listenAndServe
- Not returning a response to HTTP requests
import { serve } from "https://deno.land/std/http/server.ts";
serve(req => {
if (new URL(req.url).pathname === "/health") {
return new Response("Healthy", { status: 200 });
} else {
return new Response("Not Found", { status: 404 });
}
}, { port: 8080 });Solution
Step 1: Analyze request URL condition
The code checks if the request URL is exactly '/health'. If yes, it returns a 200 status with "Healthy".Step 2: Understand else branch
If the URL is anything else, it returns a 404 status with "Not Found".Final Answer:
Response with body 'Healthy' and status 200 when URL is '/health' -> Option BQuick Check:
URL '/health' = 200 Healthy [OK]
- Confusing status codes for different URLs
- Assuming server crashes without syntax errors
- Mixing response body and status code
import { serve } from "https://deno.land/std/http/server.ts";
serve(req => {
console.log("Received request");
});Solution
Step 1: Check handler function
The handler function only logs to console but does not return any Response, so HTTP requests get no valid reply.Step 2: Verify other parts
The import is correct, serve accepts an options object for port, and console.log is allowed. The main issue is missing Response return.Final Answer:
The handler does not return a Response object -> Option CQuick Check:
Handler must return Response [OK]
- Forgetting to return Response in handler
- Thinking console.log sends HTTP response
- Misunderstanding serve function parameters
Solution
Step 1: Understand combined health and monitoring needs
Health checks confirm app is alive; monitoring provides metrics like uptime and request count.Step 2: Evaluate options for best practice
Create two endpoints: '/health' returns 200 OK; '/metrics' returns JSON with uptime and request count cleanly separates concerns with '/health' for status and '/metrics' for detailed data, both accessible on one server.Step 3: Reject less effective options
Use only '/health' endpoint returning plain text 'OK' without metrics lacks metrics, C lacks proper status code, D adds complexity by running two servers unnecessarily.Final Answer:
Create two endpoints: '/health' returns 200 OK; '/metrics' returns JSON with uptime and request count -> Option AQuick Check:
Separate endpoints for health and metrics = Best practice [OK]
- Mixing metrics and health in one endpoint without status code
- Ignoring metrics in health checks
- Running multiple servers unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
