Performance: Deno.serve for HTTP handling
This affects server response time and how quickly the server can handle incoming HTTP requests.
Deno.serve(req => new Response("Hello World"), { port: 8000 });
import { serve } from "https://deno.land/std/http/server.ts"; const server = serve({ port: 8000 }); for await (const req of server) { req.respond({ body: "Hello World" }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Old std/http server with manual iteration | N/A | N/A | N/A | [X] Bad |
| Deno.serve with callback handler | N/A | N/A | N/A | [OK] Good |
Deno.serve primarily do in a Deno application?Deno.serve to respond with "Hello World" to every request?new Response("Hello World"). Options A, B, and C misuse Response creation or methods.import { serve } from "https://deno.land/std/http/server.ts";
Deno.serve(req => {
if (req.url.endsWith("/json")) {
return new Response(JSON.stringify({ message: "Hi" }), {
headers: { "Content-Type": "application/json" },
});
}
return new Response("Not JSON");
});Deno.serve(async (req) => {
const data = await req.json();
return new Response(data);
});await req.json() correctly parses JSON body from the request.data is an object, causing a type error.new URL(req.url) to parse the URL and access searchParams for query parameters.url.searchParams.get('name') with fallback to 'Guest', then return a Response with the greeting string.Pass the test and this pattern moves unknown