Installing a Local Web Server
Most of the time, especially in the learning phase, you'll open examples directly in the browser — we do this by double-clicking the HTML file or using a direct link to it.
Local Files vs. Remote Files
When the web address path starts with file:///C:/, followed by the path to the file on the local hard drive, a local file is being used.
When the web address starts with http:// or https://, we're accessing files located remotely, on web servers.
Problems with Testing Local Files
Some documents won't run if you open them as local files. This can happen for several reasons:
-
They involve asynchronous requests. Some browsers (including Chrome) won't run asynchronous requests if you simply run the example from a local file. This is due to security restrictions. The term asynchronous refers to two or more objects or events that don't exist or happen at the same time (or to multiple related events that occur without waiting for the previous one to finish).
-
They involve a server-side language. Server-side programming languages, such as PHP or Python, require a special server to interpret the code and deliver the results.
Running a Simple Local HTTP Server
Python Server
To solve the problem of asynchronous requests, we need to test such examples by running them through a local web server. One of the easiest ways to do this is by using the Python SimpleHTTPServer module.
Steps:
-
Visit: python.org
-
In the "Download" section, click the link for Python "3.xxx".
-
Click python-3.xxx.exe and on the first installation page, make sure to check the box "Add Python 3.xxx to PATH";
-
Then click Install, wait for the installation to finish, and then click Close.
-
Open the Windows command prompt (Windows PowerShell or Command Prompt). If you don't know how to open the command prompt click here.
To verify Python installation, enter the following command: python -V and press enter. This should return a version number. If it does, navigate to the directory where your site or example is located using the cd command. -
To start the server in the current directory, execute the command:
python -m http.server
if it doesn't work, try
python3 -m http.server
-
By default, the contents of the directory will be served on a local web server on port 8000. You can access this server at the URL localhost:8000 in your web browser. Here you will see the directory contents listed.
☞ If something is already running on port 8000, you can choose another port by running the server command followed by an alternative port number, e.g. python -m http.server 7200. You can then access your content at localhost:7200.
Running Server-Side Languages Locally
The Python server is useful, but it doesn't know how to run code written in languages like Python or PHP. Solving this depends on the server-side language you're trying to run. Here are a few examples:
-
To run the server-side language Python, you'll need to use a Python web framework. You can learn how to use the Django framework by reading – Django Web Framework (Python).
- To run server-side JavaScript code using Node.js, you'll need to use a framework. Express is a good choice – see Express Web Framework (Node.js / JavaScript).
-
To run the server-side language PHP, you need to use one of the distributions XAMPP or WampServer, which include Apache, PHP, MySQL and other applications in a single installation file.
Congratulations! This introductory course ends here. In the next course, you'll learn about the HTML markup language. Good luck and happy learning!