Discover how to make your website load images and styles effortlessly without writing extra code!
Why Serving static files in Deno? - Purpose & Use Cases
Imagine you want to show images, styles, or scripts on your website by manually reading each file and sending it over the network every time someone visits.
Manually handling each file is slow, repetitive, and easy to mess up. You might forget to set the right file type or miss a file, causing broken pages or errors.
Serving static files with a framework lets you automatically deliver images, CSS, and JavaScript files correctly and quickly without writing extra code for each file.
const file = await Deno.readFile('./image.png'); response.body = file; response.headers.set('Content-Type', 'image/png');
app.use(staticFiles('./public')); // Files served automatically from ./public folder
You can focus on building your app while the framework handles delivering all your static content smoothly and efficiently.
When you visit a blog, the pictures, fonts, and styles load instantly because the server automatically serves these static files without extra work.
Manually sending files is slow and error-prone.
Static file serving automates delivering images, styles, and scripts.
This makes websites faster and easier to build.
Practice
Solution
Step 1: Understand what static files are
Static files include images, CSS stylesheets, and JavaScript files that do not change dynamically.Step 2: Identify the purpose of serving static files
Serving static files means sending these files from the server to the user's browser so the webpage looks and works correctly.Final Answer:
To send images, styles, and scripts to the user's browser -> Option AQuick Check:
Static files = images, styles, scripts [OK]
- Confusing static files with dynamic server code
- Thinking static files run on the server
- Mixing static files with database operations
Solution
Step 1: Identify the correct module for serving static files
The function serveFile is provided by the file.ts module in Deno's standard HTTP library.Step 2: Match the import path
The correct import path is "https://deno.land/std/http/file.ts" for serveFile.Final Answer:
import { serveFile } from "https://deno.land/std/http/file.ts"; -> Option CQuick Check:
serveFile import = file.ts [OK]
- Using server.ts instead of file.ts
- Importing from fs module incorrectly
- Wrong URL path for the import
/image.png?
import { serve } from "https://deno.land/std/http/server.ts";
import { serveFile } from "https://deno.land/std/http/file_server.ts";
serve(async (req) => {
const url = new URL(req.url);
if (url.pathname === "/image.png") {
return await serveFile(req, "./static/image.png");
}
return new Response("Not Found", { status: 404 });
});Solution
Step 1: Check the URL path condition
The code checks if the requested path is exactly "/image.png".Step 2: Understand serveFile usage
If the path matches, serveFile sends the file "./static/image.png" as the response.Final Answer:
The server sends the file located at ./static/image.png to the user -> Option BQuick Check:
Path matches "/image.png" so serveFile sends file [OK]
- Assuming 404 is returned for existing path
- Thinking serveFile causes error here
- Ignoring the URL pathname check
import { serve } from "https://deno.land/std/http/server.ts";
import { serveFile } from "https://deno.land/std/http/file_server.ts";
serve(async (req) => {
const url = new URL(req.url);
if (url.pathname = "/style.css") {
return await serveFile(req, "./static/style.css");
}
return new Response("Not Found", { status: 404 });
});Solution
Step 1: Check the if condition syntax
The condition uses a single equals sign (=), which assigns instead of compares.Step 2: Understand the impact of assignment in if
Using = in if always evaluates to true, causing incorrect logic and possible bugs.Final Answer:
The assignment operator (=) is used instead of comparison (===) in the if condition -> Option DQuick Check:
Use === for comparison, not = [OK]
- Confusing = with === in if statements
- Forgetting to await serveFile (though it is awaited here)
- Incorrect import paths
public in Deno. Which approach correctly serves any requested file from that folder, falling back to a 404 if the file doesn't exist?
import { serve } from "https://deno.land/std/http/server.ts";
import { serveFile } from "https://deno.land/std/http/file_server.ts";
serve(async (req) => {
const url = new URL(req.url);
try {
// Which line correctly serves the file?
return await ???;
} catch {
return new Response("Not Found", { status: 404 });
}
});Solution
Step 1: Understand dynamic file serving
To serve any file requested, the server must use the URL path to build the file path inside the public folder.Step 2: Match the URL pathname to the file path
Using `./public${url.pathname}` correctly maps the request path to the file location.Final Answer:
serveFile(req, `./public${url.pathname}`) -> Option AQuick Check:
Dynamic path = ./public + url.pathname [OK]
- Serving only a fixed file like index.html
- Using query parameters instead of pathname
- Hardcoding a single file path
Ready to prove it?
Pass the test and this pattern moves unknown
