Servers ensure smooth operation of websites and for that to happen, developers need to configure servers using server-side code. Now, what do we mean by server-side code? Well, server-side codes are essential parts of any website that works with a database. So, if your site has a database, it also has server-side codes. No matter which server-side code your website uses, you need a server to execute these codes. This server can be either of the two types – local and remote. Here, we will talk about the local test server, why it is needed, and how you can set up a test server locally on your machine.
What is a Test Server?
Server testing is like a lifebuoy for an app developer. It is used to determine whether the services you’re utilizing for the app development process are working properly or not. Setting up a test server will help you test your website and web apps without any vexation. Typically, if the services are working fine, the server would run all the tests properly. If not, the test cases would fail and show errors. You could run these tests using certain scripts.
Why Does one Require a Test Server Anyway?
Now, you might think, “Do I even need a test server anyway”? Is it even necessary?
Well, if your website uses anything more than fully static HTML, then yes. You need a test server to trial the workability of your website’s code locally. Despite your expertise in programming, you’re still very much vulnerable to errors. And mostly, these errors don’t show up until it’s too late. It’s always better to test your local files out and a test server is going to help you with that.
Isn’t it Possible to Test Code Files Directly on the Website?
The thought of testing every code implementation on the website is pretty cringeworthy. Well, you can actually test your code files by directly applying them to your live website. But, even if you have tons of expertise, we recommend you not to test new code or code changes directly on the website. Why? It is because you’re not immune to errors. In a split second, anything could go south. Even a few errors can cause damage and hamper the functioning of your website.
What Can One Do on a Local Test Server?
A local server can do tons of things. You can use your local test server to test out code before you implement them on your site. Anything you want to include in your site should pass through a rigorous testing routine. It will stave off any errors from showing later on.
Be it the shopping cart, a new layout, or a new review system that you are implementing on a website; everything should be tested on a local server.
Setting up a Local Testing Server
Prerequisites
- Basic knowledge of the function of a web server.
Local files vs Remote files
You can launch most of your examples in your web browser. Now, how can you open an example in your browser? Well, there are two ways. First, you can double-click on the .html file and drag it to your browser’s navigation bar. And second, go to File and then open the .html file.
If the path of your URL begins with file://, it means that the file is a local one. Contrarily, if the path of your URL begins with http:// or https://, it signifies a remote file.
Common Issues that Occur while Testing Local Files
Some examples will have problems opening if you try to launch them as local files. There can be numerous causes responsible for such phenomena. Here are two of them:
- The requests these local files feature are non-synchronous in nature. Web browsers like Google Chrome usually have trouble running non-synchronous requests if you’re launching an example as a local file. This happens mainly because of the web security feature of these browsers.
- Local files contain server-side languages like JavaScript or Go. These languages need a dedicated server that can read the code and provide you with the intended results.
Running the Local Web Server
As we said earlier, local files tend to show many problems when we try to open them. To avoid the problem of non-synchronous requests, we must test them using a local HTTP server. You can test your files with ease using Python’s SimpleHTTPServer or http.server.
Here’s how to do it:
- First, you need to install Python on your PC. In most Linux and Mac systems, Python comes pre-installed. Windows users have to download it manually. Visit Python’s official website www.python.org. Check the download section of the website and follow the weblink to download the latest edition of Python (3.9.5 at present). Next, click on the Windows installer (32-bit) or Windows installer (64-bit) according to your PC. After the download has finished, launch it. On the installer page, make sure to tick the box that says “Add Python 3.9.5 to PATH”. Press the Install button.
- Launch the command line and use the following command to verify if Python has been installed on your PC properly:
python -V
# Or you might have the py command available, in that case, try running py –V.
- The output you would get will show you the version of Python. If everything is fine, Type the cd command to open the directory where your example is:
# include the directory name to enter it, for example
cd Desktop
# use two dots to jump up one directory level if you need to
cd ..
- Next, run the following command:
# If Python version returned above is 3.X
python3 -m http.server
# On windows try “python” instead of “python3”, or “py -3”
# If Python version returned above is 2.X
python -m SimpleHTTPServer
The above command will start the local server and run the directory files on that server (port 8000). One can also access this server by navigating to the web address localhost:8000 on the browser. In case your port 8000 is running something else, you can set a different port easily. All you have to do is replace port number 8000 with another port number of your choice. For example, to use port 7800, you have to type python3 -m http.server 7800 or python -m SimpleHTTPServer 7800. Your server will be available at localhost:7800.
Running Server-side Codes Locally
SimpleHTTPServer or http.server cannot run codes written using PHP, JavaScript, or Python properly. To run such codes without any issues, you got to put in extra work.
- If your server-side code is written in Python, a Python framework could come in handy. You can use any Python framework you want. But we will recommend Django as it is currently the most popular web framework for Python.
- Similarly, you can run Node.js examples using a JS web framework, such as Express.
- For PHP server-side codes, you can run the following code to use the built-in PHP development server:
$ cd path/to/your/php/code
$ php -S localhost:8000
Conclusion
Being a web developer, you wouldn’t want to face problems arising out of async requests while opening local files. So, it is always best to test these files out using a local testing server. Now that you know the method of setting up such a server, it’s time to test your codes. Good luck!
People are also reading: