Challenge - 5 Problems
Static File Serving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat is the output when serving a static file with this Deno code?
Consider this Deno server code snippet that serves a static file named
index.html. What will the server respond with when a client requests the root URL /?Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; import { serveFile } from "https://deno.land/std@0.177.0/http/file_server.ts"; serve(async (req) => { if (new URL(req.url).pathname === "/") { return await serveFile(req, "./index.html"); } return new Response("Not Found", { status: 404 }); });
Attempts:
2 left
💡 Hint
Think about what serveFile does and how the server handles the root path.
✗ Incorrect
The serveFile function reads the file and returns a Response with the file content and status 200. Since the path is '/', the server returns the file content correctly.
📝 Syntax
intermediateWhich option correctly serves a static file with Deno's serveFile?
Identify the correct syntax to serve a file named
style.css when the request path is /style.css.Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; import { serveFile } from "https://deno.land/std@0.177.0/http/file_server.ts"; serve(async (req) => { const url = new URL(req.url); if (url.pathname === "/style.css") { // Fill in the correct return statement here } return new Response("Not Found", { status: 404 }); });
Attempts:
2 left
💡 Hint
Remember that serveFile returns a Promise and needs the request object.
✗ Incorrect
serveFile requires the request object and the file path, and it returns a Promise, so it must be awaited.
🔧 Debug
advancedWhy does this Deno server code fail to serve static files correctly?
This code attempts to serve static files from a folder named
public. Why does it fail to serve files requested by the client?Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; import { serveFile } from "https://deno.land/std@0.177.0/http/file_server.ts"; serve(async (req) => { const url = new URL(req.url); const filePath = `./public${url.pathname}`; try { return await serveFile(req, filePath); } catch { return new Response("File not found", { status: 404 }); } });
Attempts:
2 left
💡 Hint
Think about what happens if the URL path is '/' or a directory.
✗ Incorrect
If the URL path is a directory or does not correspond to a file, serveFile throws an error. The code does not handle directory requests or check file existence before serving.
🧠 Conceptual
advancedWhat is the main advantage of using Deno's serveFile for static files?
Why is it better to use Deno's built-in serveFile function instead of manually reading files and creating responses for static file serving?
Attempts:
2 left
💡 Hint
Think about what headers and HTTP features static files need.
✗ Incorrect
serveFile sets proper Content-Type headers based on file extension and supports HTTP features like range requests, which improves performance and compatibility.
❓ state_output
expertWhat is the response status and body when requesting a missing static file?
Given this Deno server code that serves files from the
assets folder, what will be the HTTP status and response body if a client requests /missing.png which does not exist?Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; import { serveFile } from "https://deno.land/std@0.177.0/http/file_server.ts"; serve(async (req) => { const url = new URL(req.url); try { return await serveFile(req, `./assets${url.pathname}`); } catch (error) { if (error instanceof Deno.errors.NotFound) { return new Response("File not found", { status: 404 }); } return new Response("Internal Server Error", { status: 500 }); } });
Attempts:
2 left
💡 Hint
Check how the code handles Deno.errors.NotFound exceptions.
✗ Incorrect
When serveFile throws a NotFound error, the catch block returns a 404 response with the body 'File not found'.
