Running a web server with npx http-server
Since we installed npm
, there is an easy solution: we can have npm
run a web server for us.
In a terminal, navigate to the folder where your index.html
file is saved (./web/tmp
, but it doesn't really matter).
Then type the following:
npx http-server -c-1 .
This will run the npx program "http-server", which will run a simple web server on your computer.
- The
-c-1
argument means "never cache content, always re-fetch it". That's very important -- it means that whenever you save a file and then refresh your browser, you'll get the latest version. - Finally, the
.
argument means "let this folder be the root for all files that are served", where "this folder" is the present working directory from which you executed thenpx
command.
Anyhow, when you run it, you should see something like this:
Take note of the section after "Available on", since it tells you how to access your web site.
In my case, the site is available on http://127.0.0.1:8082, so I can put that in my browser's address bar:
When we load the page, the terminal where npx
is running will print some more output:
Indeed, every time we refresh, npx
will let us know that it responded to the request and served some new content.
Also, note that there is an error that we can ignore: the browser asked for a "favicon", to show in the address bar, but we don't have one. That's fine during development.
[2024-09-09T09:33:06.924Z] "GET /favicon.ico" Error (404): "Not found"