Challenge - 5 Problems
Deno HTTP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat is the output of this Deno.serve handler?
Consider this Deno server code. What will the client receive when accessing the root path?
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve((req) => { const url = new URL(req.url); if (url.pathname === "/") { return new Response("Welcome Home", { status: 200 }); } return new Response("Not Found", { status: 404 }); });
Attempts:
2 left
💡 Hint
Check the URL path and the response status codes returned.
✗ Incorrect
The handler checks if the request path is '/', then returns a 200 response with 'Welcome Home'. Otherwise, it returns 404 with 'Not Found'.
📝 Syntax
intermediateWhich option correctly uses Deno.serve to respond with JSON?
You want to create a Deno server that responds with JSON {"message": "Hello"}. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember to stringify JSON and set the correct Content-Type header.
✗ Incorrect
Option A correctly stringifies the object and sets the Content-Type header. Option A passes an object directly to Response which expects a string or Uint8Array. Option A misses the Content-Type header.
❓ state_output
advancedWhat is the output after multiple requests to this Deno.serve handler?
This server counts how many times it was accessed. What will the client see on the third request?
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; let count = 0; serve(() => { count += 1; return new Response(`Count: ${count}`); });
Attempts:
2 left
💡 Hint
The variable count is outside the handler function.
✗ Incorrect
The count variable is declared outside the handler and increments on each request. On the third request, count is 3.
🔧 Debug
advancedWhy does this Deno.serve code cause a runtime error?
Identify the cause of the error in this code snippet:
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve(async (req) => { const data = await req.json(); return new Response(`Hello, ${data.name}`); });
Attempts:
2 left
💡 Hint
Check the Request object methods in Deno's HTTP server.
✗ Incorrect
The Request object in Deno's serve handler is a standard Request, but it does not have a json() method. You must read the body as text or arrayBuffer and parse JSON manually.
🧠 Conceptual
expertHow does Deno.serve handle concurrency for incoming HTTP requests?
Which statement best describes how Deno.serve manages multiple incoming HTTP requests?
Attempts:
2 left
💡 Hint
Think about how JavaScript and Deno handle async operations.
✗ Incorrect
Deno.serve uses the asynchronous event loop model. It calls the handler function for each request without blocking, allowing concurrent handling of multiple requests.
