Search code examples
npmhttpserver

Making `http-server` visible across the network


Running npm's http-server will sometimes launch a server accessible from a (mobile) device on the same network.

> http-server
Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://10.0.1.7:8080
  http://123.456.78.90:8080
Hit CTRL-C to stop the server

and sometimes launch a server that is visible only from the same desktop.

> http-server
Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://10.0.1.7:8080
Hit CTRL-C to stop the server

How do I use http-server to launch a web server that's visible from any device on the same network? The presence of http://10.0.1.7:8080 should by itself indicate that the server is visible across the network, no? Why does the third address http://123.456.78.90:8080 appear only sometimes. Both launchings are identical.

All this follows npm install -g http-server of course.

Update 1:

http-server -a 0.0.0.0 is the default. (Hence it need not be specified.)

Update 2:

Using

python3 -m http.server --bind 0.0.0.0

is more predictable/reliable, but then one has to go muck around in ifconfig's output to find the IP revealed so nicely by npm's http-server.


Solution

  • It should always be available to anyone in the same network as your computer under your computer's IP followed by the port.

    The third address, I assume, appears because at the time you were connected to two local networks at the same time, so that the http server would be available at that IP - which is also the IP of your computer, but in the second network.

    So in short:

    • http://127.0.0.1:8080 is the IP that is used to connect to the server from WITHIN the machine it runs on.
    • http://10.0.1.7:8080 and any subsequent IP is the IP used to connect to the server from any device that is in the same network. As many IP's appear as how many local networks you are connected to.

    This has nothing to do with npm, it's just how the network works. Python's http server just doesn't list the IP's for you.