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 the npx command.

Warning

You should never use npx http-server in a production setting. It's just for local testing.

Anyhow, when you run it, you should see something like this:

Launching npx

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:

The same web page, but served via HTTP

When we load the page, the terminal where npx is running will print some more output:

Launching npx

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"