When it comes to http servers, there are quite a few choices out there. Some even have their own books on configuration options. But sometimes we just want to quickly open a bare minimum HTTP server to test the piece of JavaScript code we just wrote. In this lesson, we will see how we can do exactly that. We have prepared a basic demo HTML file that contains a button, a div jQuery for helping us do some Ajax calls, and the script that will try to load static content from our server and put the content inside the deep. The script is trying to load the file on the disk such file, which is nothing more than a simple text file.
If we open this file inside our browser, we can see our page content. clicking on the button generates a JavaScript error. It is telling us that we want to do a cross origin request Which is not allowed by default by the browser. This is to prevent cross site scripting attacks. What we need to do in order to test our JavaScript code is to serve this file in our HTTP server. In order to start the HTTP server in the same folder as the file.
We type the commands on the screen. This is a basic Python module that opens Port 8000, on localhost, serving only static content. So no, you can't use it for PHP. Let's open the address in the browser. Click on the button, we see that our file content was loaded in the deep beneath the button, which means the browser is no longer blocking us because we are issuing requests by the same host using the same protocol. Looking at the output from our Python server, we can see all the requests the browser has made to the server.
We can see its request By default, a favicon dot Ico file, which doesn't exist, and it's giving back a 404 status code. You can find the files used in this project on the GitHub project page. Also, if we stop the server and go one level up and fire it up again, we can use it as a web dev server having the possibility to navigate through the files in the current directory. We could for example, give access to a folder on our local machine to a remote user and allow him or her to access it through a page in the browser, eliminating the need to install file server