Deno's HTTP server uses web standards to make it easy and familiar for developers to build web apps. It helps code work smoothly across different platforms and tools.
Why Deno's HTTP server uses web standards
Introduction
Syntax
Deno
import { serve } from "https://deno.land/std/http/server.ts"; serve((request) => { return new Response("Hello from Deno HTTP server!", { status: 200, headers: { "content-type": "text/plain" }, }); });
The
serve function takes a handler that receives a Request object and returns a Response object.Both
Request and Response follow the web standard Fetch API used in browsers.Examples
Deno
serve((request) => {
return new Response("Hello World", { status: 200 });
});/json path and 404 for others.Deno
serve((request) => {
const url = new URL(request.url);
if (url.pathname === "/json") {
return new Response(JSON.stringify({ message: "Hello JSON" }), {
headers: { "content-type": "application/json" },
});
}
return new Response("Not Found", { status: 404 });
});Sample Program
This program starts a simple HTTP server using Deno's web standard APIs. It responds with different messages based on the URL path.
Deno
import { serve } from "https://deno.land/std/http/server.ts"; console.log("HTTP web server running. Access it at: http://localhost:8000/"); serve((request) => { const url = new URL(request.url); if (url.pathname === "/") { return new Response("Welcome to Deno HTTP server using web standards!", { headers: { "content-type": "text/plain" }, }); } else if (url.pathname === "/hello") { return new Response("Hello there!", { headers: { "content-type": "text/plain" }, }); } else { return new Response("Page not found", { status: 404 }); } });
Important Notes
Using web standards means you can use the same Request and Response objects on both client and server.
This approach reduces confusion and helps reuse code between browser and server.
Deno's HTTP server is easy to learn if you already know how web browsers work.
Summary
Deno's HTTP server uses web standards to keep things simple and familiar.
This helps developers write code that works well across different environments.
It makes sharing code between client and server easier and reduces bugs.
Practice
1. Why does Deno's HTTP server use web standards like the Fetch API?
easy
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 AQuick 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
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 CQuick 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
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 BQuick 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:
import { serve } from "https://deno.land/std/http/server.ts";
serve(req => {
req.respond({ body: "Hello" });
});medium
Solution
Step 1: Check the method used to send response
The code calls req.respond, which is from older Deno APIs and not compatible with the web standard serve function.Step 2: Understand the correct response pattern
In the current Deno HTTP server, the handler must return a Response object instead of calling req.respond.Final Answer:
Using req.respond is incorrect; should return a Response object -> Option AQuick Check:
Return Response, don't call req.respond [OK]
Hint: Return Response object, don't call req.respond [OK]
Common Mistakes:
- Using old Deno APIs with new serve function
- Forgetting to return a Response
- Confusing async handling with req.respond
5. How does using web standards in Deno's HTTP server help reduce bugs when sharing code between client and server?
hard
Solution
Step 1: Understand the role of shared APIs
Using the same web standard APIs on client and server means developers write similar code for both sides.Step 2: Recognize how this reduces bugs
When APIs match, fewer surprises happen due to different behaviors or missing features, reducing bugs.Final Answer:
Because both environments use the same APIs, reducing differences and surprises -> Option DQuick Check:
Same APIs = fewer bugs [OK]
Hint: Same APIs client/server means fewer bugs [OK]
Common Mistakes:
- Thinking Deno converts code automatically
- Believing web standards force sync code
- Assuming web standards eliminate all runtime errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
