Run your site on a real server
Double-clicking index.html opens a file. It does not visit a website. In this lesson you turn your own computer into a web server, learn to read the address it gives you, watch it answer requests, and use that server to load your site's content from a data file with fetch().
Everything here stays on your computer. Nobody else can see your site until you publish it in the next lesson. That makes this the safe place to test.
This page came from a server.
Before you start
- A project folder with
index.html,style.cssandscript.jsat the top, from your first site or a starter pack. - A terminal: the one built into VS Code (Ctrl+`) is fine.
- One of these: the Live Server extension, Node.js, or Python. Ask your teacher which one your computer is allowed to use.
No install allowed at all (a locked school laptop or a Chromebook)? Read along and do both labs on this page. They run on the class site's own server. Web Studio previews your files without a server.
Double-clicking is not a server
When you double-click a file, the browser's address bar starts with file://. There is no server: the browser reads the file straight off your disk. Simple pages look fine that way, so it is easy to think nothing is missing. Then you add one modern feature and it fails with a red error you did not cause.
A web server is a program that waits for a browser to ask for a file, finds it, and sends it back with a short label saying what it is. When that program runs on your own computer, the address starts with http://localhost. That is how every real website is delivered, so it is the honest way to test.
| What your site does | Double-clicked (file://) | Served (http://localhost) |
|---|---|---|
| Show HTML, CSS and images with relative paths | Works | Works |
Load a data file with fetch('projects.json') | Blocked in most browsers | Works |
Use JavaScript modules (<script type="module">, import) | Blocked in most browsers | Works |
A path that starts with /, like /style.css | Means the top of your whole drive | Means the top of your site folder |
| Service workers (sites that work offline) | Not available | Works: localhost counts as a secure address |
| Auto-reload when you save | No | Yes, with Live Server |
The fetch() failure is the one you will meet first. In Chrome the console says something like this:
Access to fetch at 'file:///…/my-site/projects.json' from origin 'null'
has been blocked by CORS policy: Cross origin requests are only
supported for protocol schemes: http, data, https … Read it calmly. Origin 'null' means "this page came from no server". The fix is not in your code. Start a server and open the page through it.
Read a localhost address
Every server prints an address when it starts. It has the same four parts as any web address, and each part is worth knowing:
http://localhost:8080/pages/about.html
| Part | Here | What it means |
|---|---|---|
| Scheme | http | How to talk to the server. Locally, plain http is fine because the request never leaves your computer. |
| Host | localhost | Which computer. localhost is a name that always means this computer. 127.0.0.1 is the same thing written as a number. |
| Port | 8080 | Which program on that computer. One computer runs many programs that talk to the network, so each one listens on its own numbered port. Only one program can hold a port at a time. Live Server usually picks 5500, npx serve 3000, Python whatever you tell it. |
| Path | /pages/about.html | Which file, counted from the folder the server was started in. That folder becomes /. |
The last row is the one that causes trouble. If you start the server in the folder above your site, every address needs an extra folder name in it, and your /style.css links break. Always start the server inside the folder that holds index.html.
Start a server: pick one way
All three do the same job. Use whichever your computer already has. First, in the terminal, move into your site folder and check that index.html is there:
cd my-site
ls On Windows Command Prompt, use dir instead of ls. If you don't see index.html in the list, you're in the wrong folder. Fix that before going on.
| Tool | Start it | Open | Stop it |
|---|---|---|---|
| VS Code Live Server | Open the folder in VS Code, then click Go Live in the status bar (or right-click index.html → Open with Live Server). | It opens the browser for you, usually at http://127.0.0.1:5500/. It reloads every time you save. | Click the port number in the status bar. |
| Node.js | npx serve .The first time, it asks to download the serve package. Type y. | The Local: address it prints, usually http://localhost:3000 | Ctrl+C in the terminal |
| Python | python3 -m http.server 8000Windows: py -m http.server 8000 | http://localhost:8000 | Ctrl+C in the terminal |
Check: the address bar starts with http://, your page has its styles, and your button still works.
Repair: command not found means that tool isn't installed, so try another row. Address already in use means another program holds that port, maybe a server you forgot to stop. Stop the old one, or pick another number, like python3 -m http.server 8001.
A terminal that is running a server is busy. It won't take new commands until you stop the server. Open a second terminal tab for Git and other commands.
Watch the server answer
A server that you start in a terminal prints one line for every request it answers. Load your page once, then look. Python's log looks like this:
127.0.0.1 - - [19/Nov/2026 10:42:01] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2026 10:42:01] "GET /style.css HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2026 10:42:01] "GET /script.js HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2026 10:42:01] "GET /images/Logo.png HTTP/1.1" 404 - One page is several requests: the HTML first, then every file it links to. Each line says who asked (127.0.0.1 or ::1, both mean you), what they asked for (GET /style.css) and the status code the server answered with. The browser's Network panel (F12 → Network) shows the same list from the browser's side.
| Code | Meaning | What to do |
|---|---|---|
| 200 OK | Found it; here it is. | Nothing. |
| 304 Not Modified | The browser already had this file and it hasn't changed, so the server sends nothing new. | Nothing. If your change doesn't show up, do a hard reload (Ctrl/Cmd+Shift+R). |
| 301 Moved Permanently | You asked for a folder without the final /. Go to the address with the slash. | Nothing; the browser follows it. |
| 404 Not Found | No file has that exact name at that exact path. | Compare the path in the log with your real filename, letter by letter. |
Lab: Be the server
On the left is a site folder, one file per line. Type an address and this lab answers it with the rules a simple server follows. Change the files, the address, or the case setting, and predict the answer before you look.
Request → file → answer
Check: you can explain why / sends index.html, why Logo.png is a 404 only when the case box is ticked, and what happens to the ?sort=new and #top parts.
Try this: put every file under my-site/ (as if you started the server one folder too high) and request / again. That file list is what most "my site is just a list of files" problems look like.
Load your content with fetch()
So far your project cards have been typed into index.html. Many real sites keep the content in a separate data file instead, and JavaScript builds the cards from it. Add a project by adding three lines to the data file, and every page that uses it updates. That only works through a server, which is why it's here.
projects.json
[
{
"title": "Orbit study",
"kind": "Simulation",
"description": "A moon circles a planet."
},
{
"title": "Star catcher",
"kind": "Game",
"description": "Catch falling stars."
}
] JSON is strict: double quotes only, and no comma after the last item. One stray comma and the whole file fails to read.
script.js
async function showProjects() {
const list = document.querySelector('#projects');
try {
const response = await fetch('projects.json');
if (!response.ok) {
throw new Error(`${response.status} for projects.json`);
}
const projects = await response.json();
for (const project of projects) {
const card = document.createElement('article');
const title = document.createElement('h3');
title.textContent = project.title;
const text = document.createElement('p');
text.textContent = project.description;
card.append(title, text);
list.append(card);
}
} catch (error) {
list.textContent = 'Projects could not load: ' + error.message;
}
}
showProjects(); In your HTML, give it somewhere to go: <section id="projects"></section>.
fetch()asks the server for a file, the same way the browser asked forstyle.css. The answer takes time, soawaitwaits for it without freezing the page.awaitonly works inside anasyncfunction.- A 404 is still an answer.
fetch()doesn't fail when the file is missing. It returns a response withokset to false. Always checkresponse.ok. response.json()turns the text into real JavaScript arrays and objects.textContent, notinnerHTML, puts the words on the page. If the data ever contains<or a script, it shows as text instead of running.- The
catchputs the problem on the page. A visitor should see "Projects could not load", not a mysteriously empty section.
Lab: a real fetch() from this site's server
This lab really asks the class site's server for a file, then shows exactly what came back. The path is relative to this lesson page, just as yours will be relative to your index.html.
Press Fetch it.
The code that ran
What the server sent back
Check: the data file gives 200 OK and three cards. Each other button fails for a different reason, and you can name the reason.
Running this lesson from a laptop instead of the class site? Wrong capital P may load there, because most laptops ignore capital letters. On the class site, a Linux server, it's a 404. That's the trap from the case box in the first lab.
Your turn: make a projects.json in your own site folder with your real projects, add the script above, and open the site through your server. Then open it by double-clicking instead and read the console error. Now you know what it looks like.
Data from other sites
You can fetch() from another website too (weather, space photos, museum collections), but two rules apply.
- The other site decides. A browser only lets your page read another site's answer if that site's server says so in its reply. That permission is called CORS. If an API doesn't allow it, nothing in your code can change that. Pick an API whose documentation says it works from the browser.
- No keys in your JavaScript. Many APIs hand out a secret key. Anything in
script.jscan be read by every visitor, so a key there is a key you've given away. For a class site, choose APIs that need no key at all.
Test on a phone
npx serve prints a second address, labelled Network:, such as http://192.168.1.24:3000. That is your computer's address on the Wi-Fi. A phone on the same network can open it and show your real site on a real small screen.
- Many school networks block one device from reaching another, so it may simply not load. That's the network doing its job. Use the browser's device toolbar (F12, then the phone icon) instead.
- While the server runs, anyone on that network who finds the address can read every file in the folder you served. Python and
servelisten to the whole network by default. Serve only your site folder (never your home folder or Documents) and stop the server when you finish.
What this kind of server can't do
Every server in this lesson, and GitHub Pages, is a static server. It hands out files exactly as they are on disk. It runs none of your code itself: your JavaScript runs in the visitor's browser.
A static server can
- Serve pages, styles, scripts, images, sound and video
- Serve data files your JavaScript reads with
fetch() - Run games, simulations and tools in the visitor's browser
- Remember things for one visitor in their own browser (
localStorage)
It needs a back end to
- Save a message from a contact form
- Keep a high-score table that every player shares
- Let people log in
- Keep a secret key secret
A back end is a program you write that runs on the server, usually with a database, in a language like JavaScript (Node.js) or Python. It's the next step after this course, and it brings real responsibility for other people's data. For now, be honest on your page about what a form does, as the HTML lesson asks.
Repair guide
| You see | Usually because | Fix |
|---|---|---|
| A plain list of files instead of your page | No index.html at the top of the served folder, or the server started one folder too high. | Stop it, cd into the folder that holds index.html, start again. |
| Page loads but has no styles | The CSS link's path or capital letters don't match the file. | Read the 404 line in the server log and match the name exactly. |
| "blocked by CORS policy" and origin 'null' | The page was opened by double-clicking (file://). | Open the http://localhost address instead. |
Unexpected token from response.json() | The JSON has a trailing comma or single quotes, or the path returned an HTML page. | Open the JSON file's address directly in the browser and read what comes back. |
| "Address already in use" | Another server holds that port. | Stop the old server, or use another port number. |
| Changes don't appear | The browser kept an old copy (cache), or the file isn't saved. | Save, then hard reload with Ctrl/Cmd+Shift+R. |
| The address just spins, then fails | The server was stopped, or the terminal was closed. | Look at the terminal. Start the server again. |