Javascript Game Foundations - A Web Server

Ten Essential Foundations of Javascript Game Development

Web Server

You might imagine that a pure client-side HTML5 game can be developed and tested by simply opening
your HTML pages directly from local disk in an appropriate browser, but due to various security
restrictions, that approach generally doesn’t work (e.g. you can’t make an AJAX call to load
assets from local disk)

Therefore you will need to run a rudimentary web server on your local development machine in order
to see the fruits of your labor while you are busily creating your next gaming masterpiece.

There are many, many ways to get a local webserver, depending on your technology of choice:

Personally, since I do a lot of ruby development, I am comfortable with the simple adsf
local web server:

> sudo gem install adsf
> cd my/game/directory
> adsf -H thin -p 3000

Module Structure

In addition to serving up your game during development you will also need to decide on the
structure of your code.

is the game small enough to live inside a single module?

do you want to break it up into a separate module per-class?

will you need a build tool to unify and minify your javascript?

Javascript is a very flexible language, but with that flexibility comes a multitude of choices on
how you want to pattern your application.

If your game is small and simple, it might be easiest to have a single file and use the module pattern
to keep the implementation private. I use this approach for many smaller games such as my rotating
tower game, for example:

Once the size of your codebase gets larger you will want to break it up. Best practice is to
maintain individual source files for development and then unify (and minify) them for performance
reasons, to serve up a single javascript and a single css file at run time.

However, it’s not that hard to roll-your-own. Using Ruby and Rake, I created a simple UnifiedAssets
library that will take my individual .js and .css files and combine them into a single unified
run-time file.

I can then declare a simple Rake file in my game directory that uses the UnifiedAssets library
to provide some simple rake tasks:

The rake assets:server task provided by the UnifiedAssets gem can now be used as a replacement
for the simple local web server described in the previous section, and provides the additional
benefit of automatically unifying all of my assets whenever they change: