feat: Enhance Deno server with dynamic routing and environment configuration

This commit is contained in:
Mauricio Siu 2025-02-23 20:01:01 -06:00
parent 2105b9a9ba
commit f29a6f704b
4 changed files with 127 additions and 8 deletions

BIN
deno/deno Executable file

Binary file not shown.

65
deno/deno.lock Normal file
View File

@ -0,0 +1,65 @@
{
"version": "3",
"packages": {
"specifiers": {
"jsr:@std/cli@^1.0.12": "jsr:@std/cli@1.0.13",
"jsr:@std/dotenv": "jsr:@std/dotenv@0.225.3",
"jsr:@std/encoding@^1.0.7": "jsr:@std/encoding@1.0.7",
"jsr:@std/fmt@^1.0.5": "jsr:@std/fmt@1.0.5",
"jsr:@std/html@^1.0.3": "jsr:@std/html@1.0.3",
"jsr:@std/http": "jsr:@std/http@1.0.13",
"jsr:@std/media-types@^1.1.0": "jsr:@std/media-types@1.1.0",
"jsr:@std/net@^1.0.4": "jsr:@std/net@1.0.4",
"jsr:@std/path@^1.0.8": "jsr:@std/path@1.0.8",
"jsr:@std/streams@^1.0.9": "jsr:@std/streams@1.0.9"
},
"jsr": {
"@std/cli@1.0.13": {
"integrity": "5db2d95ab2dca3bca9fb6ad3c19908c314e93d6391c8b026725e4892d4615a69"
},
"@std/dotenv@0.225.3": {
"integrity": "a95e5b812c27b0854c52acbae215856d9cce9d4bbf774d938c51d212711e8d4a"
},
"@std/encoding@1.0.7": {
"integrity": "f631247c1698fef289f2de9e2a33d571e46133b38d042905e3eac3715030a82d"
},
"@std/fmt@1.0.5": {
"integrity": "0cfab43364bc36650d83c425cd6d99910fc20c4576631149f0f987eddede1a4d"
},
"@std/html@1.0.3": {
"integrity": "7a0ac35e050431fb49d44e61c8b8aac1ebd55937e0dc9ec6409aa4bab39a7988"
},
"@std/http@1.0.13": {
"integrity": "d29618b982f7ae44380111f7e5b43da59b15db64101198bb5f77100d44eb1e1e",
"dependencies": [
"jsr:@std/cli@^1.0.12",
"jsr:@std/encoding@^1.0.7",
"jsr:@std/fmt@^1.0.5",
"jsr:@std/html@^1.0.3",
"jsr:@std/media-types@^1.1.0",
"jsr:@std/net@^1.0.4",
"jsr:@std/path@^1.0.8",
"jsr:@std/streams@^1.0.9"
]
},
"@std/media-types@1.1.0": {
"integrity": "c9d093f0c05c3512932b330e3cc1fe1d627b301db33a4c2c2185c02471d6eaa4"
},
"@std/net@1.0.4": {
"integrity": "2f403b455ebbccf83d8a027d29c5a9e3a2452fea39bb2da7f2c04af09c8bc852"
},
"@std/path@1.0.8": {
"integrity": "548fa456bb6a04d3c1a1e7477986b6cffbce95102d0bb447c67c4ee70e0364be"
},
"@std/streams@1.0.9": {
"integrity": "a9d26b1988cdd7aa7b1f4b51e1c36c1557f3f252880fa6cc5b9f37078b1a5035"
}
}
},
"remote": {},
"workspace": {
"dependencies": [
"jsr:@std/assert@1"
]
}
}

View File

@ -1,10 +1,24 @@
export function add(a: number, b: number): number {
return a + b;
}
import "jsr:@std/dotenv/load";
import { serveFile } from "jsr:@std/http/file-server";
// 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 PORT = Deno.env.get("PORT") || 8000;
Deno.serve({ hostname: "0.0.0.0", port: 8000 }, () => new Response("Hello, world!"));
const handler = async (req: Request): Promise<Response> => {
const url = new URL(req.url);
if (url.pathname === "/") {
return await serveFile(req, "./public/index.html");
} 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);
}

40
deno/public/index.html Normal file
View File

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Deno 2 Web Server</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
text-align: center;
}
h1 {
color: #4caf50;
}
p {
font-size: 1.2rem;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to Deno 2</h1>
<p>Your Deno 2 web server is up and running!</p>
<p>
Visit the <a href="/greet">/greet</a> endpoint to see a greeting
message.
</p>
</div>
</body>
</html>