Complete the code to create a simple HTTP server that responds with 'OK' for health checks.
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve((_req) => new Response("OK"), { port: [1] });
The server listens on port 3000, a common port for development servers.
Complete the code to check the health endpoint path '/health'.
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve((req) => { if (req.url.endsWith([1])) { return new Response("OK"); } return new Response("Not Found", { status: 404 }); }, { port: 3000 });
The health check endpoint is commonly '/health' to indicate service status.
Fix the error in the code to correctly parse the request URL for health check.
serve((req) => {
const url = new URL(req.[1]);
if (url.pathname === "/health") {
return new Response("OK");
}
return new Response("Not Found", { status: 404 });
}, { port: 3000 });The Request object has a 'url' property containing the full URL string.
Fill the blank to create a JSON response with status and uptime.
serve(() => {
const uptime = performance.[1]();
return new Response(JSON.stringify({ status: "OK", uptime: uptime }), {
headers: { "Content-Type": "application/json" }
});
}, { port: 3000 });performance.now() returns uptime in milliseconds since the Deno process started.
Fill both blanks to create a health check that returns status, uptime, and memory usage in JSON.
serve(() => {
const metrics = performance.[1]();
const memory = Deno.[2]();
return new Response(JSON.stringify({
status: "OK",
uptime: metrics,
memory: memory.rss
}), {
headers: { "Content-Type": "application/json" }
});
}, { port: 3000 });performance.now() provides uptime in ms, Deno.memoryUsage() provides memory info, and memory.rss is resident set size in bytes.
