Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the HTTP server from Deno's standard library.
Deno
import { serve } from "https://deno.land/std/http/[1].ts";
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module names like 'server_mod' or 'http_server'.
Forgetting the '.ts' extension.
✗ Incorrect
The correct module to import the HTTP server is 'server' from Deno's standard library.
2fill in blank
mediumComplete the code to create a basic HTTP server that responds with 'Hello World'.
Deno
serve((req) => new Response("Hello World"), { port: [1] });
Attempts:
3 left
💡 Hint
Common Mistakes
Using port 80 without proper permissions.
Choosing ports not typically used for HTTP.
✗ Incorrect
Port 8080 is commonly used for HTTP servers in development.
3fill in blank
hardFix the error in the code to correctly handle the request using web standards.
Deno
serve(async ([1]) => { const url = new URL(req.url); return new Response(`Path: ${url.pathname}`); });
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the one referenced inside the function.
Confusing request and response objects.
✗ Incorrect
The parameter name must match the variable used inside the function, which is 'req'.
4fill in blank
hardFill both blanks to create a response with a JSON body and correct headers.
Deno
serve((req) => {
const data = { message: "Hello" };
return new Response(JSON.stringify(data), { headers: { '[1]': '[2]' } });
});Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Accept' header instead of 'Content-Type'.
Setting content type to 'text/plain' for JSON data.
✗ Incorrect
The header 'Content-Type' must be set to 'application/json' to indicate JSON response.
5fill in blank
hardFill all three blanks to correctly parse JSON from a POST request and respond with a message.
Deno
serve(async (req) => {
if (req.method === '[1]') {
const body = await req.json();
const name = body.[2] || 'Guest';
return new Response(`Hello, ${name}!`, { status: [3] });
}
return new Response('Method Not Allowed', { status: 405 });
});Attempts:
3 left
💡 Hint
Common Mistakes
Checking for GET instead of POST.
Using wrong JSON key.
Returning wrong status code.
✗ Incorrect
The server checks for POST method, extracts 'name' from JSON body, and responds with status 200.
