Introduction
Deno.serve lets you quickly create a simple web server to handle HTTP requests and send responses. It makes building web apps easy without extra setup.
Deno.serve(async (request) => { // handle request and return response return new Response("Hello World", { status: 200 }); });
Deno.serve(() => new Response("Hello from Deno!"));Deno.serve(async (req) => { if (req.method === "GET") { return new Response("You sent a GET request"); } else { return new Response("Only GET allowed", { status: 405 }); } });
Deno.serve(async (req) => { const url = new URL(req.url); if (url.pathname === "/hello") { return new Response("Hello there!"); } return new Response("Not found", { status: 404 }); });
Deno.serve(async (request) => { const url = new URL(request.url); if (url.pathname === "/") { return new Response("Welcome to the homepage!", { headers: { "content-type": "text/plain; charset=utf-8" }, }); } else if (url.pathname === "/about") { return new Response("This is the about page.", { headers: { "content-type": "text/plain; charset=utf-8" }, }); } else { return new Response("Page not found", { status: 404 }); } });
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