Did you find this helpful?

Running Test HTTP Server

In some scenarios you may need to run local HTTP server for test purposes. For example, if you are following our Facebook & HTML5Google & HTML5, or Getting Started with Twitch & HTML5 Guides and you have not remote server with a domain, you may find this guide useful.


Installing HTTP server using NPM

Run the command line/terminal on your system. It does not matter which directory you are currently in. Execute 'npm install -g http-server'. Once npm finishes, you have tiny http-server installed. That's it.

Serving files

Create a new folder on your system from where you want to server your static files. There are no specific restrictions for path on Windows. On Mac OS you may want to check for access on the folder. Inside of newly created folder create a file called 'index.html'

Fill the file with the following content:

<!doctype html>

<html lang="en">
  <meta charset="utf-8">
  <title>My Page</title>

 <p>Hello world!</p>

Run the command line/terminal and cd to the folder with your 'index.html' file. Execute 'http-server' (1). Http server will start serving files from your current directory (2). It will also print out all the IP endpoints the server is listening for (3). Use one of them to reach your web-server through the browser (4). Observe your page being loaded (5):

You can optionally specify port to run the server on. For example, execute `http-server -p 80` to run your server on port 80. If port is occupied, you will have to find what application is using this port and shut it down, before you attempt to run the HTTP-server again.

Serving files with custom domain name

When testing different SDKs, for example Google or Facebook you will notice that some services can only be configured with a certain valid domain name. I.e. they do not accept plain IP address + port. They use such configuration to ensure your browsers fetch your code from a certain, secure domain. They also often use this configuration to ensure that user can only log into your application from your domain and nobody else can fake it and steal user data. While this is important and useful security restriction, it may produce complications when trying to locally test your code.

If you run your server on port 80 and you have and your use-case is simple enough, you can utilize HOSTS file on your operating system to access your HTTP server through a valid domain name. HOSTS file is a special file available on both Windows and Mac OS. It allows you to override certain domain names with custom endpoints. Schematically, HOSTS file looks like this:


# This is comment 
# IP_ADDRESS_4 DOMAIN_NAME_4 <- this entry is commented out and is inactive

Each line represents exactly one entry. When you try to reach DOMAIN_NAME_1, your browser will direct your request to IP_ADRESS_1. The same applies for each entry in the HOSTS file. In the example above, entry number 4 starts with '#'. This is a syntax for comment. By commenting out certain entries you may disable them, without necessarily removing them from the hosts file.

'playfab.example' is valid domain name, but unlikely your browser will reach any website with it. By adding the following entry to your HOSTS file, you will be able to reach your local http server with that domain name: playfab.example

On Windows OS, HOSTS file is normally located at `C:\Windows\System32\drivers\etc\hosts`.

On Mac OS, HOSTS file is normallylocated at `/private/etc/hosts`

Warning: when entering you domain name in your browser address bar, always make sure to include schema: http://playfab.example

Did you find this helpful?