Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the serve function from Deno's standard HTTP library.
Deno
import { serve } from "https://deno.land/std/http/[1].ts";
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module names like 'server1' or 'server2' which do not exist.
✗ Incorrect
The correct import path is 'server' to access the HTTP server utilities in Deno's standard library.
2fill in blank
mediumComplete the code to start the server listening on port 8080.
Deno
serve(() => new Response("Hello"), { port: [1] });
Attempts:
3 left
💡 Hint
Common Mistakes
Using ports like 3000 or 443 without proper context or permissions.
✗ Incorrect
Port 8080 is commonly used for HTTP servers during development and is the correct port here.
3fill in blank
hardFix the error in the code to correctly create a Response with a JSON body.
Deno
return new Response(JSON.stringify({ message: "Hi" }), { headers: { 'Content-Type': [1] } });
Attempts:
3 left
💡 Hint
Common Mistakes
Setting Content-Type to 'text/plain' or 'text/html' when sending JSON data.
✗ Incorrect
The Content-Type header must be 'application/json' to indicate the response body is JSON.
4fill in blank
hardFill both blanks to create a server that responds with 'Welcome' on the root path and 404 otherwise.
Deno
serve(async (req) => {
const url = new URL(req.url);
if (url.pathname === [1]) {
return new Response("Welcome");
} else {
return new Response("Not Found", { status: [2] });
}
});Attempts:
3 left
💡 Hint
Common Mistakes
Using '/home' instead of '/' for the root path.
Using status code 200 for 'Not Found' responses.
✗ Incorrect
The root path is '/', and 404 is the standard status code for 'Not Found'.
5fill in blank
hardFill all three blanks to create a server that responds with JSON containing the request method and URL.
Deno
serve((req) => {
const data = {
method: req.[1],
url: req.[2]
};
return new Response(JSON.stringify(data), { headers: { 'Content-Type': [3] } });
});Attempts:
3 left
💡 Hint
Common Mistakes
Using 'body' instead of 'method' or 'url' properties.
Setting Content-Type to a wrong value like 'text/plain'.
✗ Incorrect
The request object has 'method' and 'url' properties, and the response must have Content-Type 'application/json' for JSON data.
