It opens on my laptop. Why not on yours? · slopware 101

slopware 101 · step 4 of 35

It opens on my laptop. Why not on yours?

by · 4 min read

A development tool starts a website on your own machine and prints an address such as localhost:3000. You click it and your page opens.

Then you send the link to a friend and it does not open for them. This chapter is about that address: what it means, why your friend gets nothing, and what has to change before anyone else can see your work.

What does localhost mean?

localhost is a name reserved to mean the computer that is asking. When your machine looks it up, the answer is a loopback address: 127.0.0.1 in IPv4, or ::1 in IPv61. Most machines have both, and which one a program tries first depends on the operating system and the program. Either way the request never leaves the machine, which is why it is called loopback.

When your friend types localhost, their machine sends the request to itself. If no program is listening at that port, the connection fails; if another program is there, they may see a completely different page. Every computer's localhost is a different place.

The number after the colon is the port. If the address is the building, the port is the apartment inside it. One machine runs many programs and each waits at its own number. 3000 is a convention some development tools picked, nothing more.

For localhost:3000 to answer even you, two things have to be true. A program has to be running, which sounds obvious until you close the terminal that held the dev server and wonder where the page went. And it has to be listening on the address your browser tried. If a server listens only on 127.0.0.1 and the client tries ::1 first, some tools report the connection as refused while the server sits right there. When localhost fails and you are sure something is running, try http://127.0.0.1:3000 directly. If that works, the difference was the address, not your code.

Serving a folder to your own phone

Open the terminal, create a disposable folder containing only a harmless index.html file and cd into it, and run this.

python3 -m http.server 8000 --bind 127.0.0.1

This starts a small web server, and the flag tells it to listen on the loopback address only. Open http://127.0.0.1:8000 and you see the index.html page from that folder. Type the same address into your phone's browser and nothing comes back, even though the phone is on the same wifi, half a metre away. The phone went looking for port 8000 on itself.

Stop the server with Ctrl+C and start it again without the flag.

python3 -m http.server 8000

Without the flag, this tool listens on every network interface the machine has, including the wifi one. Now find the laptop's address on the local network: ipconfig getifaddr en0 on a Mac (try en1 if it comes back empty), hostname -I on Linux, or ipconfig on Windows, where you want the line called IPv4 Address. It looks like 192.168.1.24. Type http://192.168.1.24:8000 into the phone and the same index.html page appears. If it does not, a firewall on the laptop may be blocking it, or the network may keep devices apart, which guest and cafe networks often do.

Nothing in the files changed. Only which addresses the program listens on, called its bind address, and which machine the phone was told to ask.

Every tool picks a default bind address, and they do not all pick the same one. Startup output often lists local and network addresses. Confirm the bind setting in the tool's configuration instead of treating the displayed URL as proof. A network bind can make the server reachable by other devices where firewall and network rules allow it. Run this sharing exercise on a network you control, then stop the server with Ctrl+C.

Binding is not deploying

So I drop the flag and I am live, you may be thinking. What you have is a server reachable from the same network. 192.168.1.24 is a private address that means something only inside your home network, so a friend in another city or a customer on mobile data cannot reach it. If the laptop sleeps, its development server stops answering; the page has not moved to another machine.

A product has to answer while you are asleep, so the program has to run on a machine that stays on and has an address the internet can route to. Putting it there is called deploying, and it gets its own chapter later. Changing the bind address decides who on your network may knock. It does not move the building.

Can anyone else open your project?

Look at the address bar of the project you are proudest of. If it begins with localhost, 127.0.0.1 or [::1], that address refers to the machine opening it. For a project you have deployed, close the laptop, switch your phone to mobile data, and try its public URL. That checks whether the deployed copy can answer independently of your development machine.

The next chapter is about what runs on the other end once the project does leave your machine, and why some of your code can never live in the browser.

Further reading: Localhost in RFC 6761.


  1. IPv4 and IPv6 are two generations of internet addressing. IPv4 addresses look like 127.0.0.1, IPv6 addresses like ::1. Both are in use, often on the same machine.↩︎