Challenge - 5 Problems
Deno HTTP Server Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediateWhy does Deno's HTTP server use web standards?
Deno's HTTP server is designed to follow web standards like the Fetch API and Web Streams. What is the main benefit of this approach?
Attempts:
2 left
💡 Hint
Think about how using the same APIs on client and server helps developers.
✗ Incorrect
Deno uses web standards so that developers can write code that works similarly on both client and server. This reduces learning time and improves code sharing.
❓ component_behavior
intermediateWhat happens when you use Deno's HTTP server with the Fetch API?
Consider this Deno HTTP server code snippet:
const server = Deno.serve({ port: 8000 });
for await (const req of server) {
req.respondWith(new Response('Hello World'));
}
What is the behavior of this server when accessed by a browser?
Attempts:
2 left
💡 Hint
Check what the Response object does in web standards.
✗ Incorrect
The server uses the Fetch API's Response object to send back a plain text response 'Hello World' to every request.
📝 Syntax
advancedIdentify the syntax error in this Deno HTTP server code
What is the syntax error in the following Deno HTTP server code?
const server = Deno.serve({ port: 8080 });
for await (const request of server) {
request.respondWith(Response('Hi'));
}
Attempts:
2 left
💡 Hint
How do you create a new Response object in JavaScript?
✗ Incorrect
The Response object must be created with the 'new' keyword. Omitting it causes a TypeError.
❓ state_output
advancedWhat is the output when streaming a response in Deno's HTTP server?
Given this code snippet:
const server = Deno.serve({ port: 9000 });
for await (const req of server) {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode('Hello '));
controller.enqueue(new TextEncoder().encode('Stream!'));
controller.close();
}
});
req.respondWith(new Response(stream));
}
What will the client receive?
Attempts:
2 left
💡 Hint
ReadableStream sends data chunks to the client as they are enqueued.
✗ Incorrect
The server streams the text 'Hello ' and then 'Stream!' to the client, which combines to 'Hello Stream!'.
🔧 Debug
expertWhy does this Deno HTTP server code cause a runtime error?
Examine this code:
const server = Deno.serve({ port: 7000 });
for await (const req of server) {
const url = new URL(req.url);
if (url.pathname === '/hello') {
req.respondWith(new Response('Hello!'));
} else {
req.respondWith(new Response('Not found', { status: 404 }));
}
}
When running, it throws: "TypeError: Cannot read property 'url' of undefined". What causes this error?
Attempts:
2 left
💡 Hint
Check what 'req' actually represents in Deno.serve() iteration.
✗ Incorrect
In Deno's HTTP server, the iteration yields RequestEvent objects, not raw requests. The 'req' variable does not have a 'url' property directly.
