Based on Chapter 20, except that we are not using cookies, sessions, view templates, or having Facebook integration.

Today is more web dev than game dev. Not exactly my favorite, mostly because troubleshooting here is a whole new world, but we'll be getting somewhere fun soon...

Today's server application code on BitBucket

Today's server application running on Heroku


Server application reads and writes data to and from a persistent layer such as a database.

We'll be using MongoDB because it's incredibly simple and has a great JavaScript integration.

MongoDB/Node.js Links

About MongoDB

  • Document-oriented storage database, which means you can store arbitrary documents and data in it without needing to explicitly craft a database schema as you would with a traditional relational database system (MySQL, etc).
  • Can be installed locally, which along with Node.js installed locally makes testing online services locally pretty easy.
  • Like a RDBMS, MongoDB supports having multiple databases on a single installation
  • Instead of tables it has collections. Once you start using a collection, MongoDB creates them if necessary.

Installing MongoDB locally

  • Discussed on page 396 of the book
  • Win32: Download is a zip, and you can install MongoDB in any directory you choose.
  • Create your data folder used by MongoDB. Default is :\data\db. You can pass in another path as a command argument
  • Start the database:

Accessing a database

  • You can now connect to the database through an interactive JavaScript shell to test it out:
  • By default, mongo looks for a database server listening on port 27017 on the localhost interface, and uses the 'test' database for context.
  • Note: a database won't be created until you actually insert data into that database

Basic Shell Commands:

  • db - tells you the name of the current database
  • show dbs - shows all databases
  • use databaseName - sets the current database (default is 'test')
  • help - shows valuable helpful help things
  • - every command has a .help()
  • db[ collectionName ].find() - returns the entire collection
  • db[ collectionName ].save( {} ); - adds a new entry to the collection
  • db[ collectionName ].drop() - empties out the entire collection

Try these commands in the interactive shell

				// switch databases
				use blob

				// Creating Records{ user: "Tester", clicks: 5 }){ user: "Tester 2", clicks: 15 }){ user: "Tester 3", clicks: 10 })

				// Querying the collection
				db.clicks.find({ user: "Tester" })
				db.clicks.findOne({ user: "Tester 2" })
				db.clicks.find().sort({ clicks: -1 }).limit(2)

				// Destroy a collection

Updating our Node.js server application

Build a web server application that demonstrates database access: insert/update/remove/query.

  • Pull in the mongodb driver (Node.js module)
  • Use Express for static file hosting
  • Connect to the database
  • Connect to the 'users' collection and define a few utility functions to accessing the database:
    • fetchUser - returns an entry in the collection
    • setUserScore - updates or adds an entry in the collection
    • topTen - returns a sorted list of entries from the collection
    • clearUsers - empties the collection
  • Setup our routes
    • index.html - handles checking if there is post data to add or update the "users" collection with.
    • top-ten - grabs JSON data for the top ten scores in the "users" collection.
    • clearUsers - empties the "users" collection.
  • Callback heavy! This can get crazy hard to follow in a larger application. This is why we generate the helper functions, though we could also use the promise pattern from Chapter 8 of the book.

Hosting our Node.js app online with Heroku

Finding a hosting service:

Heroku Links

Heroku Overview

Heroku does not serve files, it runs a server application which can serve files (or not).

Three major components for what we'll put on Heroku to run our Node.js server application:

  • Dependencies in packages.json
  • Application source code in a javascript file.
  • Process types in a Procfile - Heroku uses this to know what to do

Heroku uses environment variables for things like port and database name, that we can check in our Node.js application.

Getting Started

  1. Create Heroku account. You'll need to verify a credit card for your account to use addons, even when using free ones like MongoHQ.
  2. Install Heroku toolbelt - this gives you command lines options for accessing Heroku.
  3. Done!

Testing Locally

  • Install our dependencies
    npm install
  • Login to Heroku
    heroku login
  • Use "foreman" to test our Heroku Node.js application locally.
    foreman start

Testing Online

  • Make sure your server application is under Git source control
    git init
    git add -A
    git commit -m "Server application, initial commit"
  • Login to Heroku
    heroku login
  • Create a new application online. This generates a random URL name and a git name (only need to do this once)
    heroku create
  • Setup your git to handle deployment to Heroku (only need to do this once)
    git remote add heroku
  • Add mongoDB to your Heroku (only need to do this once) (You can also do this through the dashboard)
    heroku addons:add mongohq:sandbox
  • Push your server application to Heroku. This takes a while because it puts our application into the cloud and intalls our dependencies on Heroku.
    git push heroku master
  • Check out your application running online!
    heroku restart

Recommended Homework

Basically, be able to make changes to you webapp.js, restart the server (local or online), repeat!


  • Install Node.js and MongoDB
  • Start your database: mongod.exe (make sure you've setup a database folder). Leave it running.
  • Build your server application: package.json, webapp.js
  • Install your dependencies: npm install
  • Test your server application locally through node: node webapp.js


  • Create Heroku account
  • Install Heroku toolbelt
  • Create Heroku app, and add your server application code to git
  • Test your app locally through foreman
  • Push your application to Heroku
  • Test your app online