Bird
Raised Fist0
Denoframework~20 mins

Deno.serve for HTTP handling - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Deno HTTP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What 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 });
});
AThe client receives a 404 status with body 'Not Found'.
BThe client receives a 200 status with body 'Welcome Home'.
CThe client receives a 200 status with body 'Not Found'.
DThe client receives a 404 status with body 'Welcome Home'.
Attempts:
2 left
💡 Hint
Check the URL path and the response status codes returned.
📝 Syntax
intermediate
2:00remaining
Which 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?
Aserve(() => new Response(JSON.stringify({ message: "Hello" }), { headers: { "Content-Type": "application/json" } }));
Bserve(() => Response.json({ message: "Hello" }));
Cserve(() => new Response({ message: "Hello" }, { headers: { "Content-Type": "application/json" } }));
Dserve(() => new Response(JSON.stringify({ message: "Hello" })));
Attempts:
2 left
💡 Hint
Remember to stringify JSON and set the correct Content-Type header.
❓ state_output
advanced
2:00remaining
What 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}`);
});
ACount: 0
BCount: 1
CCount: 3
DCount: undefined
Attempts:
2 left
💡 Hint
The variable count is outside the handler function.
🔧 Debug
advanced
2:00remaining
Why 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}`);
});
AThe Response constructor requires a second argument.
BMissing Content-Type header in the response.
CThe handler must not be async.
Dreq.json() is not a function on the Request object.
Attempts:
2 left
💡 Hint
Check the Request object methods in Deno's HTTP server.
🧠 Conceptual
expert
3:00remaining
How does Deno.serve handle concurrency for incoming HTTP requests?
Which statement best describes how Deno.serve manages multiple incoming HTTP requests?
ADeno.serve runs the handler concurrently for each request using asynchronous event loop, allowing multiple requests to be handled at the same time.
BDeno.serve processes each request sequentially, waiting for one to finish before starting the next.
CDeno.serve creates a new thread for each incoming request to handle them in parallel.
DDeno.serve queues requests and processes only one request per CPU core at a time.
Attempts:
2 left
💡 Hint
Think about how JavaScript and Deno handle async operations.

Practice

(1/5)
1. What does Deno.serve primarily do in a Deno application?
easy
A. It creates a simple HTTP server that listens for requests.
B. It compiles TypeScript code into JavaScript.
C. It manages database connections automatically.
D. It formats JSON data for API responses.

Solution

  1. Step 1: Understand the purpose of Deno.serve

    Deno.serve is designed to start an HTTP server that listens for incoming requests.
  2. Step 2: Compare options with this purpose

    Only It creates a simple HTTP server that listens for requests. describes creating an HTTP server. Other options describe unrelated tasks.
  3. Final Answer:

    It creates a simple HTTP server that listens for requests. -> Option A
  4. Quick Check:

    Deno.serve creates HTTP server [OK]
Hint: Deno.serve starts a server to handle HTTP requests [OK]
Common Mistakes:
  • Confusing Deno.serve with code compilation
  • Thinking it manages databases
  • Assuming it formats data automatically
2. Which of the following is the correct way to use Deno.serve to respond with "Hello World" to every request?
easy
A. Deno.serve(() => new Response.text("Hello World"));
B. Deno.serve(request => Response("Hello World"));
C. Deno.serve(() => Response.text("Hello World"));
D. Deno.serve(() => new Response("Hello World"));

Solution

  1. Step 1: Check the correct Response object creation

    In Deno, to send a response, you create a new Response object with the body text.
  2. Step 2: Validate syntax of each option

    Deno.serve(() => new Response("Hello World")); correctly uses new Response("Hello World"). Options A, B, and C misuse Response creation or methods.
  3. Final Answer:

    Deno.serve(() => new Response("Hello World")); -> Option D
  4. Quick Check:

    Use new Response() to send reply [OK]
Hint: Always use new Response() to send HTTP replies [OK]
Common Mistakes:
  • Omitting 'new' keyword before Response
  • Using Response as a function instead of a constructor
  • Calling non-existent Response.text method
3. What will the following code output when accessed via a browser?
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");
});
medium
A. The browser shows: Not JSON
B. The browser shows: {"message":"Hi"} with JSON content type
C. The browser shows a 404 error
D. The server crashes with an error

Solution

  1. Step 1: Analyze the request URL condition

    The code checks if the URL ends with '/json'. If yes, it returns JSON; otherwise, it returns "Not JSON".
  2. Step 2: Determine the output for a normal browser request

    A browser accessing the root URL (e.g., http://localhost:8000/) does not end with '/json', so it returns "Not JSON" as plain text.
  3. Final Answer:

    The browser shows: Not JSON -> Option A
  4. Quick Check:

    URL ends with '/json'? No = "Not JSON" [OK]
Hint: Check URL endings carefully to predict response [OK]
Common Mistakes:
  • Assuming all requests return JSON
  • Ignoring URL path in condition
  • Expecting server errors without syntax issues
4. Identify the error in this Deno.serve usage:
Deno.serve(async (req) => {
  const data = await req.json();
  return new Response(data);
});
medium
A. Cannot call req.json() on a Request object
B. Response constructor expects a string or Uint8Array, not an object
C. Missing await before new Response()
D. Deno.serve does not support async functions

Solution

  1. Step 1: Understand req.json() usage

    Calling await req.json() correctly parses JSON body from the request.
  2. Step 2: Check Response constructor argument

    The Response constructor expects a string or binary data, but data is an object, causing a type error.
  3. Final Answer:

    Response constructor expects a string or Uint8Array, not an object -> Option B
  4. Quick Check:

    Response body must be string or bytes, not object [OK]
Hint: Always stringify objects before passing to Response [OK]
Common Mistakes:
  • Passing raw object to Response instead of string
  • Thinking req.json() is invalid on Request
  • Assuming async functions are unsupported
5. You want to create a Deno server that responds with "Hello, {name}!" where {name} is a query parameter from the URL (e.g., /?name=Alice). Which code snippet correctly implements this?
hard
A. Deno.serve(req => { const url = req.url; const name = url.params.name || 'Guest'; return new Response(`Hello, ${name}!`); });
B. Deno.serve(req => { const name = req.query.name || 'Guest'; return new Response(`Hello, ${name}!`); });
C. Deno.serve(req => { const url = new URL(req.url); const name = url.searchParams.get('name') || 'Guest'; return new Response(`Hello, ${name}!`); });
D. Deno.serve(req => { const name = req.get('name') || 'Guest'; return new Response(`Hello, ${name}!`); });

Solution

  1. Step 1: Extract query parameters from request URL

    Use new URL(req.url) to parse the URL and access searchParams for query parameters.
  2. Step 2: Get 'name' parameter safely and respond

    Use url.searchParams.get('name') with fallback to 'Guest', then return a Response with the greeting string.
  3. Final Answer:

    Deno.serve(req => { const url = new URL(req.url); const name = url.searchParams.get('name') || 'Guest'; return new Response(`Hello, ${name}!`); }); -> Option C
  4. Quick Check:

    Use URL and searchParams to read query [OK]
Hint: Parse req.url with URL to access query parameters [OK]
Common Mistakes:
  • Assuming req has query or params properties
  • Trying to get query params directly from req
  • Using non-existent req.get() method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard