How to serve local web files over HTTP for testing

Recently I got interested in creating my own webapp. If you write a simple html file you can open it with a browser. Notice that in the browser in the little window in which you normally type a URL it says ‘file://’. This tells you that the file is not served over the HTTP protocol. As consequence your webpage might not work as expected. Some more information on this topic is here. To test it properly you want to use a HTTP-server and have your page served via HTTP.

There are various ways to do so, I tried the following:

  1. set up an apache server
  2. use node.js
  3. use python (recommended!)

1. Setting up an apache server to test a simple webpage is a horribly stupid idea. You will waste – or invest, depending on your point of view – a lot of time for understanding the workings of the various configuration files of apache. I do not recommend this!

2. I used node.js in the following way:

  • In the directory containing the ‘index.html’ file, create a file called ‘server.js‘. Run ‘npm init’ and answer questions for ‘package.json’. Then run ‘node server.js’ to start the HTTP-server. Call ‘localhost:1026’ in your browser. That’s the specified port in server.js. Adapt if needed.

3. python: If you have python installed type ‘python -m http.server’ in the directory with your index.html. That’s it.

Was this helpful?

0 / 0