refactor: Simplify Deno server with minimal example and basic add function

This commit is contained in:
Mauricio Siu 2025-02-23 20:07:12 -06:00
parent cfdd6f5fdd
commit d525138542

View File

@ -1,23 +1,10 @@
export function add(a: number, b: number): number {
return a + b;
}
const PORT = Deno.env.get("PORT") || 8000;
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
console.log("Add 2 + 3 =", add(2, 3));
}
const handler = async (req: Request): Promise<Response> => {
const url = new URL(req.url);
if (url.pathname === "/") {
const greeting = Deno.env.get("GREETING") || "Hello from Deno 2!";
return new Response(greeting);
} else if (url.pathname === "/greet") {
const greeting = Deno.env.get("GREETING") || "Hello from Deno 2!";
return new Response(greeting);
} else {
return new Response("Not Found", { status: 404 });
}
};
// Error handling
try {
Deno.serve({ port: Number(PORT) }, handler);
} catch (err) {
console.error("Error starting the server:", err);
}
Deno.serve({ hostname: "0.0.0.0", port: 8000 }, () => new Response("Hello, world!"));