# Why do I need a local server for HTML5 game development?

I'm new to HTML5 game development. I'm trying to make something using Pixi.js, but when I tried to view it in a browser, I saw a blank page. According to the tutorial I'm following, I need to set up a local server. I'm not sure why I'd need to do that, though.

Could you please tell me why I would need a local server to run HTML files on my browser?

• For general html you don't need a local server, and you can simply run the file in your browser. Javascript is all client-side, so you could do everything inside of the browser. Other languages are server side, like php, and they require a server. Who's telling you you need a local server? – ultifinitus Jul 3 '14 at 18:43
• I guess I must have miss-read somewhere and mashed up a bunch of information. I tried to load the Pixi.js but what resulted was a blank page. I am also following this tutorial (flippinawesome.org/2013/11/04/…) where he says to setup a local server – Question-Everything Jul 3 '14 at 18:51
• It is not a bad idea to have a local server (probably something like xampp instead of bare apache) because the environment will be consistent for when you launch your production site. But you certainly don't need one! – ultifinitus Jul 3 '14 at 18:54
• You don't need one for basic features, but you do for a bunch of specific things (eg. how browsers access certain resources) so just download xampp from www.apachefriends.org and be done with it. – jhocking Jul 3 '14 at 18:57

Web browsers disable some Javascript features for security reasons.

My bet is that you just hit Google Chrome forbidding XMLHttpRequest on local files by default (see this answer on SO for how to disable that, but beware: this is dangerous). See this page on the Chrome dev site for details.

This is why you have to setup a local HTTP server to workaround the security lock.

If you just open the game's root html-file in a browser, your browser will (hopefully) not allow that page to access any other files. This is a security feature that limits damage from malicious scripts.

If you're comfortable on a command line and happen to have Python version 2 installed, an easy way to run a local web server is to change to the desired root directory and run

python -m SimpleHTTPServer


which serves the currenty directory's contents on localhost port 8888, as documented here. Subdirectories are served, parent directories aren't, so things stay contained nicely.

The Python 3 equivalent is python -m http.server. (Thanks Mike!)

• If you're running Python 3 then you'll want to use python -m http.server [port] source – Mike Cluck Jul 7 '14 at 15:24

I've had similar issues myself in the past.

## Serving Pages

What your HTML file is possibly doing is sending HTTP requests to Localhost in order to load resources (such as Pixi.js), not actually requesting files. If not, Pixi.js may be doing that itself.

This is useful behavior on a web server, as some of the things it might be requesting could be generated dynamically, differ in response to cookies, or similar. XMLHttpRequests often go to this kind of dynamic content, and in this case the computer handling the request needs to know what to do with the file in order to generate the proper response. (If they didn't, then server-side scripts would be unable to run, you'd just get a copy of the file.) That is what the web server is there to handle.

## The Flip Side

Similarly, would you want pages from the internet to be able to access all your local files? Run arbitrary scripts as they see fit? As other answers have implied, it is a huge security risk. That's why it's turned off by default. By setting up a web server you can allow access to specific files and not others, as well as specify how things are accessed or run.

## What to do?

In your case, it's not so handy. If there is a webserver recommended in the tutorial, I'd use that. If not, I use Lighttpd. There's also the python SimpleHTTPServer mentioned in another answer.

Hope this helps.

Because of browsers' default security settings

A web browser has some extra security settings on when viewing an HTML page from local storage (like opening an HTML file with your browser). Part of these settings is to disallow AJAX requests (javascript requesting other pages). Part of this has to do that AJAX requests are always scoped to the same domain (http://example.com can only perform AJAX requests on http://example.com) and a local HTML file has no domain.

Possibly your HTML5 game framework requests its assets via AJAX in order to be more responsive.

By booting up a local web server to serve the files, the browser has a domain to scope the various AJAX requests to thus conforming with the security logic of the browser.