Web Development Pro | Lesson 052 | Static Hosting, Paths, and HTTP Delivery
Web Development Pro · Lesson 52 of 60
Module 6: Delivery and the capstone
In this lesson
Prerequisites and learning outcomes
Complete lesson 51 first. This lesson builds on base paths, mime types, and case sensitivity. By the end, you can diagnose why a local site may fail after being hosted under a subdirectory. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
A hosted URL changes how paths resolve
A site that works at the server root may fail when published under /hub/. A root-relative reference such as /assets/styles.css starts at the origin root, while assets/styles.css is resolved relative to the current document's directory. Choose paths based on the intended location rather than on what happened to work in one local folder.
Our example contains index.html, events/index.html, and assets/styles.css. The home page links to assets/styles.css. The nested events page links to ../assets/styles.css. Both resolve correctly when the entire folder is served at a root or under a subdirectory, because the relationships inside the folder remain the same.
Work through URL resolution explicitly
const pageURL = 'https://learning.example/hub/events/index.html';
console.log(new URL('../assets/styles.css', pageURL).pathname);
console.log(new URL('/assets/styles.css', pageURL).pathname);
The first path is /hub/assets/styles.css. The second is /assets/styles.css. The second may point outside the deployed application folder. The supplied Node tests verify these calculations using a reserved example domain without making requests.
A trailing slash also matters. Resolving a relative link from /hub/events differs from resolving it from /hub/events/. In the first case, events can be treated as the last path segment to replace; in the second, it is a directory segment. Hosting redirects and directory-index behavior should be explicit and tested.
Distinguish document-relative and module-relative assets
A fetch call with a relative string usually resolves against the document's base URL in a browser. That may surprise you if the module lives in a different directory. When data intentionally sits beside a module, construct its URL from import.meta.url so the relationship is explicit.
const dataURL = new URL('./events.json', import.meta.url);
Here import.meta.url is the current module's URL. If the module is at /hub/assets/app.mjs, the data URL becomes /hub/assets/events.json. This technique does not change CORS rules or authorize a request; it only makes path resolution clear.
The lesson's path-check module calculates this relationship without fetching. The capstone later uses the same pattern for local JSON loading. Keeping calculation and delivery checks separate lets you identify whether a wrong URL or a server response caused a failure.
Respect exact filename case
Some local filesystems ignore case differences while many hosting environments distinguish them. A link to Styles.css may appear to work locally even though the file is named styles.css. Use consistent lowercase filenames and match every reference exactly. Test on the actual delivery environment when publication is authorized.
Also check spaces and special characters. URL encoding and filesystem names are related but not identical concepts. Simple stable filenames reduce avoidable mistakes. Do not rename assets late in a release without updating all references and rerunning the link checks.
A static checker can verify that a referenced file exists in the source tree. It cannot prove the host uploads that file, serves the expected bytes, or applies the expected case rules. Both source integrity and actual delivery belong in the release evidence.
Inspect status, type, and body together
A response status describes an HTTP outcome, but a 200 response is not sufficient proof that a module or JSON file is correct. Some hosts return an HTML fallback page for unknown paths. A module request receiving that HTML can fail because its media type and content are wrong even though the status says success.
HTML should be served with an appropriate text/html type, CSS with text/css, JSON with application/json, and JavaScript with a supported JavaScript media type such as text/javascript. Module loading has strict type expectations. A text/plain or HTML response is not repaired merely by naming the local file .mjs.
Inspect the request's final URL, status, Content-Type, and response body in an available browser's Network panel. If a module body starts with an HTML document, investigate a missing path or fallback configuration. Do not rewrite valid JavaScript to compensate for a server delivering the wrong file.
Plan multipage and missing-page behavior
This project uses real HTML pages, so navigating directly to events/index.html should work without a client-side router. A missing path should return an appropriate not-found response rather than silently masquerading as a successful unrelated page. A custom 404 page can still give visitors useful navigation.
A single-page application may deliberately use a routing fallback, but that configuration must distinguish application routes from missing asset requests. Copying such a fallback into a small multipage site can hide broken links and create misleading successful statuses. Choose behavior that matches the actual architecture.
Test direct navigation, refresh, and a copied deep link. A path reached by clicking from home may work while a direct request fails because the server has a different route rule. Include both routes in the smoke checklist instead of relying only on one guided tour.
Use local serving without overclaiming hosting
If an existing Python installation and environment permit it, run python -m http.server 8000 --bind 127.0.0.1 from the example folder. The loopback binding limits this practice server to the local machine. It is a development aid, not a production server recommendation or a substitute for HTTPS hosting checks.
The supplied checks parse HTML references and calculate URLs in Node. They do not claim that a server was started or that any live MIME type, redirect, or status was verified. Browser launch was unavailable in the authoring environment, so those delivery observations remain a manual gate.
Worked practice
Worked exercise 1: Repair a subdirectory asset
Task: /hub/events/index.html requests /assets/styles.css and loses styling. Worked answer: if the asset belongs at /hub/assets/styles.css, use ../assets/styles.css from that nested page. Confirm the resulting URL, then verify the host actually serves the file with text/css.
Worked exercise 2: Diagnose a successful wrong response
Task: An app.mjs request returns 200 with an HTML body. Worked answer: inspect missing-file and fallback rules. The status alone is not enough; a module needs the correct JavaScript content and type. Repair the delivery route instead of treating the HTML as a syntax bug in your module.
Worked exercise 3: Locate data beside a module
Task: A module is shared by home and nested pages. Worked answer: use new URL('./events.json', import.meta.url) if the JSON lives beside that module. This keeps the asset relationship stable across document locations while preserving normal browser request policies.
Knowledge check
Question 1
What does /assets/styles.css resolve against? A) The current module folder. B) The origin root. C) Always the current page folder. D) The folder containing the CSS file itself.
Check question 1: answer and explanation
Answer: B. The initial slash makes it root-relative within the origin.
Question 2
Why test exact filename case? A) Hosts may distinguish names that a local filesystem treats alike. B) The browser automatically lowercases every requested path. C) A valid MIME type makes differently cased filenames equivalent. D) The module extension makes local and host case rules identical.
Check question 2: answer and explanation
Answer: A. Matching source names avoids portability failures.
Question 3
What does import.meta.url identify? A) The HTML document URL even when the module is in a subfolder. B) The origin root only. C) The current module's URL. D) The latest fetch response URL.
Check question 3: answer and explanation
Answer: C. It can anchor assets deliberately stored beside a module.
Question 4
Which observation best verifies a module response? A) Status 200 alone. B) A filename ending in .mjs alone. C) A cached console message alone. D) Expected final URL, status, JavaScript media type, and body.
Check question 4: answer and explanation
Answer: D. Delivery correctness needs several aligned signals.
Question 5
What does the local path test prove? A) The live host serves the correct MIME types. B) The tested URL-resolution rules produce expected paths. C) All deployment redirects work. D) The server has a correct 404 policy.
Check question 5: answer and explanation
Answer: B. Calculating a destination is separate from observing its response.