Bird
Raised Fist0
Denoframework~5 mins

Deno.serve for HTTP handling - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is Deno.serve used for in Deno?

Deno.serve is a simple way to create an HTTP server that listens for requests and sends responses.

Click to reveal answer
beginner
How do you handle incoming HTTP requests using Deno.serve?

You provide a function to Deno.serve that takes a Request object and returns a Response object.

Click to reveal answer
beginner
What does this code do?
await Deno.serve(req => new Response('Hello'))

This code starts an HTTP server that responds with 'Hello' to every request it receives.

Click to reveal answer
intermediate
Can Deno.serve handle asynchronous operations inside the request handler?

Yes, the request handler function can be async, allowing you to await operations like database calls before sending a response.

Click to reveal answer
intermediate
How do you specify a custom port when using Deno.serve?

You pass an options object as the first argument with a port property, like Deno.serve({ port: 8080 }, handler).

Click to reveal answer
What does the function passed to Deno.serve receive?
AA Socket object
BA Response object
CA Server object
DA Request object
How do you send a plain text response using Deno.serve?
AReturn <code>new Response('text')</code>
BCall <code>res.send('text')</code>
CUse <code>console.log('text')</code>
DCall <code>request.respond('text')</code>
Which option correctly sets the server to listen on port 3000?
ADeno.serve(3000, handler)
BDeno.serve({ port: 3000 }, handler)
CDeno.serve(handler, 3000)
DDeno.servePort(3000, handler)
Can the request handler function be asynchronous in Deno.serve?
AYes, it can be async and use await
BNo, it must be synchronous
COnly if you use callbacks
DOnly if you import extra modules
What will happen if you do not specify a port in Deno.serve?
AIt throws an error
BIt listens on port 80
CIt listens on the default port 8000
DIt does not start the server
Explain how to create a simple HTTP server using Deno.serve that responds with 'Welcome!' to every request.
Think about the function signature and what it returns.
You got /4 concepts.
    Describe how you would handle an asynchronous operation, like fetching data, inside a Deno.serve request handler.
    Remember async functions and awaiting promises.
    You got /4 concepts.

      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