📖 Scenario: You are learning how Deno's HTTP server works. Deno uses web standards like the Fetch API to handle requests and responses. This makes the server code familiar and easy to understand for anyone who knows web programming.
🎯 Goal: Build a simple HTTP server in Deno that uses the Fetch API to respond to requests. You will see how using web standards makes the server code clean and easy to read.
📋 What You'll Learn
Create a basic HTTP server using Deno's standard library
Use the Fetch API's Request and Response objects
Handle incoming requests and send back a simple text response
Use async functions and web standard patterns
💡 Why This Matters
🌍 Real World
Web servers built with Deno can use familiar web APIs, making it easier for web developers to write backend code without learning new server-specific APIs.
💼 Career
Understanding how Deno uses web standards prepares you for modern backend development and helps you write clean, maintainable server code that aligns with web platform conventions.
Progress0 / 4 steps
1
Set up a basic HTTP server with Deno
Write code to import the serve function from "https://deno.land/std@0.177.0/http/server.ts" and start a server on port 8000 using serve. Assign the server to a variable called server.
Deno
Hint
Use import { serve } from "https://deno.land/std@0.177.0/http/server.ts" and then call serve with { port: 8000 }.
2
Create an async loop to handle requests
Add an asyncfor await loop that iterates over server to handle incoming requests. Use the variable name requestEvent for each iteration.
Deno
Hint
Use for await (const requestEvent of server) to handle each incoming request asynchronously.
3
Use web standard Request and Response objects
Inside the loop, get the request from requestEvent.request. Then create a new Response object with the text "Hello from Deno!" and assign it to a variable called response.
Deno
Hint
Use const request = requestEvent.request and then const response = new Response("Hello from Deno!").
4
Send the response using web standards
Call requestEvent.respondWith(response) inside the loop to send the response back to the client.
Deno
Hint
Use requestEvent.respondWith(response) to send the response.
Practice
(1/5)
1. Why does Deno's HTTP server use web standards like the Fetch API?
easy
A. To make server code familiar and easier to share with client code
B. To force developers to learn a new, unique API only for Deno
C. To improve server speed by using non-standard protocols
D. To prevent compatibility with browsers and other runtimes
Solution
Step 1: Understand the use of web standards in Deno
Deno uses web standards like the Fetch API so developers can write code that feels familiar from the browser environment.
Step 2: Recognize the benefit of sharing code
Using the same APIs on server and client makes sharing code easier and reduces bugs caused by different APIs.
Final Answer:
To make server code familiar and easier to share with client code -> Option A
Quick Check:
Web standards = Familiarity + Sharing [OK]
Hint: Web standards mean same code works client and server [OK]
Common Mistakes:
Thinking Deno uses unique APIs only
Believing web standards slow down the server
Assuming web standards prevent browser compatibility
2. Which of the following is the correct way to create a basic HTTP server in Deno using web standards?
easy
A. import { createServer } from "deno_http";
createServer((req, res) => res.end("Hello"));
B. import http from "deno_http";
http.createServer((req, res) => res.send("Hello"));
C. import { serve } from "https://deno.land/std/http/server.ts";
serve(() => new Response("Hello"));
D. import { listenAndServe } from "deno_std";
listenAndServe((req) => "Hello");
Solution
Step 1: Identify the official Deno HTTP server import
Deno's standard library provides the 'serve' function from "https://deno.land/std/http/server.ts" which uses web standards.
Step 2: Check the server creation syntax
The correct syntax is calling serve with a handler function returning a Response object, matching web standards.
Final Answer:
import { serve } from "https://deno.land/std/http/server.ts";
serve(() => new Response("Hello")); -> Option C
Quick Check:
Use 'serve' + Response for Deno HTTP server [OK]
Hint: Use 'serve' from Deno std with Response object [OK]
Common Mistakes:
Using Node.js style imports or APIs
Calling res.end or res.send which are not web standard
Importing from incorrect or non-existent modules
3. What will the following Deno HTTP server code output when accessed?
import { serve } from "https://deno.land/std/http/server.ts";
serve(req => {
if (req.method === "POST") {
return new Response("Posted!");
}
return new Response("Hello World");
});
medium
A. Always responds with "Posted!"
B. Responds with "Hello World" for GET and "Posted!" for POST
C. Throws an error because method check is invalid
D. Responds with "Hello World" for POST and "Posted!" for GET
Solution
Step 1: Analyze the request method check
The code checks if the request method is "POST" and returns "Posted!" in that case.
Step 2: Understand the default response
If the method is not "POST", it returns "Hello World". So GET requests get "Hello World".
Final Answer:
Responds with "Hello World" for GET and "Posted!" for POST -> Option B
Quick Check:
POST = "Posted!", others = "Hello World" [OK]
Hint: Check req.method to decide response text [OK]
Common Mistakes:
Assuming all requests respond with "Posted!"
Thinking method property is invalid or missing
Mixing up POST and GET responses
4. Identify the error in this Deno HTTP server code snippet: