Serving the Game with Node.js

Before your app can handle requests to save level data, the game needs to be served from something besides a static web server. One solution for this would be to write a simple PHP script that takes in the data and saves the file to disk. Because this is a book about JavaScript, however, and you’ll be using Node.js to build a multiplayer game in the next couple of chapters, it makes more sense to build the editor using Node to get more experience with it.

Creating the package.json File

Just like the spritesheet creator application from Chapter 8, “Running JavaScript on the Command Line,” the editor needs a package.json file to let Node know some details about the application, including dependencies.

Create a new directory called editor for the editor, and create a file called package.js with the contents in Listing 19-1.

Listing 19-1: The package.json file

{
    "name": "platformer-editor"
  , "version": "0.0.1"
  , "private": true
  , "dependencies": {
      "express": "2.5.8"
    , "jade": ">= 0.0.1"
    , "underscore": "1.3.3"
  }
}

This application has three dependencies, your good friend underscore.js; a Node.js application framework called Express; and its dependency, jade.

Setting Up Node to Serve Static Assets

Node.js provides a minimal baseline of functionality for processing web requests. To get it to do something such as serve static files, you need to pull in a module. There are a number of different modules you could use whose only purpose is to serve static files, ...

Get Professional HTML5 Mobile Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.