Complete the code to import the static file server from Deno's standard library.
import { serveFile } from "https://deno.land/std/http/file_server.ts"; // [1]
The serveFile function is imported from file_server.ts to serve static files.
Complete the code to create a basic HTTP server that listens on port 8000.
import { serve } from "https://deno.land/std/http/server.ts"; const server = serve({ port: [1] }); console.log("HTTP server running on http://localhost:8000/");
Port 8000 is commonly used for local development servers and matches the console log message.
Fix the error in the request handler to serve the requested file correctly.
for await (const req of server) { const response = await serveFile(req, new URL(`./public${new URL([1]).pathname}`, import.meta.url)); req.respondWith(response); }
req.url which does not existreq.path which does not existreq.request.path which does not existThe req.request.url property contains the full URL. The pathname is extracted to construct the static file path.
Fill both blanks to correctly handle errors when serving static files.
for await (const req of server) { try { const response = await serveFile(req, new URL(`./public${new URL([1]).pathname}`, import.meta.url)); req.respondWith(response); } catch ([2]) { req.respondWith({ status: 404, body: "File not found" }); } }
Use req.request.url to get the full URL path and error as the catch variable to handle exceptions.
Fill all three blanks to create a complete static file server that listens on port 9000 and serves files from the 'static' folder.
import { serve } from "https://deno.land/std/http/server.ts"; import { serveFile } from "https://deno.land/std/http/file_server.ts"; const server = serve({ port: [1] }); console.log("Server running on http://localhost:9000/"); for await (const req of server) { try { const response = await serveFile(req, new URL(`./[2]$[3]${new URL(req.request.url).pathname}`, import.meta.url)); req.respondWith(response); } catch (error) { req.respondWith({ status: 404, body: "Not Found" }); } }
The server listens on port 9000, serves files from the 'static' folder, prepends '/' before the pathname extracted from req.request.url.
