This is a quick post on how I was able to take the code from:
http://nowjs.com/examples/map
And get it running in node.js using express and jade. Finished code located here:
https://github.com/rockhowse/nowjs-multiplayer-map-tutorial
This post assumes you have downloaded and installed node.js and npm. Check the main site for links to full insructions:
http://nodejs.org/
After node has been installed we first make our directory:
node-multiplayer-map-tutorial
The first thing I am going to do is install “express” node module:
http://expressjs.com/
We can do this using npm:
>npm install -g express
*note* -g is “gobal” so that any node.js project can use it
At this point we can use the “express” command to generate a stub node.js application:
C:\<%path_to_project%>\nowjs-multiplayer-map-tutorial>npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 200 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/mkdirp/0.0.7
npm http GET https://registry.npmjs.org/connect
npm http 200 https://registry.npmjs.org/mime
npm http 200 https://registry.npmjs.org/mkdirp/0.0.7
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.0.7.tgz
npm http 200 https://registry.npmjs.org/qs
npm http 200 https://registry.npmjs.org/connect
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.0.7.tgz
npm http GET https://registry.npmjs.org/formidable
npm http 200 https://registry.npmjs.org/formidable
C:\<%path_to_npm%>\npm\express -> C:\<%path_to_npm%>\npm\node_modules\express\bin\express
mime@1.2.4 C:\<%path_to_npm%>\npm\node_modules\express\node_modules\mime
mkdirp@0.0.7 C:\<%path_to_npm%>\npm\node_modules\express\node_modules\mkdirp
qs@0.4.0 C:\<%path_to_npm%>\npm\node_modules\express\node_modules\qs
formidable@1.0.8 C:\<%path_to_npm%>\npm\node_modules\express\node_modules\connect\node_modules\formidable
connect@1.8.5 C:\<%path_to_npm%>\npm\node_modules\express\node_modules\connect
express@2.5.2 C:\<%path_to_npm%>\npm\node_modules\express
At this point we can now run the “express” command to create our node.js stub code:
C:\<%path_to_project%>\nowjs-multiplayer-map-tutorial>express
create : .
create : ./package.json
create : ./app.js
create : ./public
create : ./views
create : ./views/layout.jade
create : ./views/index.jade
create : ./public/stylesheets
create : ./public/stylesheets/style.css
create : ./public/javascripts
create : ./public/images
create : ./routes
create : ./routes/index.js
dont forget to install dependencies:
$ cd . && npm install
As stated in the output, we need to install the dependencies for this express web project:
C:\Users\Sexbox\Desktop\nodejs\nowjs-multiplayer-map-tutorial>npm install
npm http GET https://registry.npmjs.org/jade
npm http GET https://registry.npmjs.org/express/2.5.2
npm http 200 https://registry.npmjs.org/express/2.5.2
npm http GET https://registry.npmjs.org/express/-/express-2.5.2.tgz
npm http 200 https://registry.npmjs.org/jade
npm http GET https://registry.npmjs.org/jade/-/jade-0.20.0.tgz
npm http 200 https://registry.npmjs.org/express/-/express-2.5.2.tgz
npm http 200 https://registry.npmjs.org/jade/-/jade-0.20.0.tgz
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/mkdirp/0.0.7
npm http GET https://registry.npmjs.org/connect
npm http GET https://registry.npmjs.org/commander
npm http GET https://registry.npmjs.org/mkdirp
npm http 304 https://registry.npmjs.org/mkdirp/0.0.7
npm http 304 https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/connect
npm http 200 https://registry.npmjs.org/commander
npm http GET https://registry.npmjs.org/formidable
npm http 200 https://registry.npmjs.org/mkdirp
npm http 304 https://registry.npmjs.org/formidable
jade@0.20.0 ./node_modules/jade
├── commander@0.2.1
└── mkdirp@0.2.1
express@2.5.2 ./node_modules/express
├── mkdirp@0.0.7
├── mime@1.2.4
├── qs@0.4.0
└── connect@1.8.5
At the time of this writing, it installs the “jade” plugin.
Nowe we can test this out by running the project using the standard node command:
C:\<%path_to_project%>\nowjs-multiplayer-map-tutorial>node app.js
Express server listening on port 3000 in development mode
You can then open up the web browser and navigate to the url below to check it out:

Here is the file breakdown at this point:

The files we will be primarily concerned with in this tutorial are as follows:
- package.json – node.js application metadata and package requirements
- app.js – main node.js application
- index.jade – main HTML page that will be rendered for the root URL (‘/’)
Why all this complexity? “express” makes it very easy to serve html and javascript dynamically using routes. Doing this by hand can get tedious.
First modification: package.json
We will add the module “now” to the package.json since this is the module that we will use for real time communication.
“dependencies”: {
“express”: “2.5.2″
, “jade”: “>= 0.0.1″
,”now”: “>=”0.7.4″
}
Once the file has been saved, lets re-run the command:
>npm install
If you are NOT on windows, it should download and install the now package under your node_modules directory. However… if you are on windows it’s a bit more complicated.
If you do the command:
C:\<path-to-project>\nowjs-multiplayer-map\npm i
You will most likely get an error with node-proxy as shown below:
npm WARN node-proxy@0.5.2 package.json: bugs['web'] should probably be bugs['url']
> node-proxy@0.5.2 install C:\<path-to-project>\nowjs-multiplayer-map\node_modules\now\node_modules\node-proxy
> make
‘make’ is not recognized as an internal or external command,
operable program or batch file.
npm ERR! error installing node-proxy@0.5.2
npm ERR! error installing now@0.7.6
npm ERR! error rolling back now@0.7.6 Error: UNKNOWN, unknown error ‘C:\<path-to-project>\nowjs-multiplayer-map\node_modules\now\node_modules\___socket.io.npm\package\lib\transports\websocket’
npm ERR! node-proxy@0.5.2 install: `make`
npm ERR! `cmd “/c” “make”` failed with 1
npm ERR!
npm ERR! Failed at the node-proxy@0.5.2 install script.
npm ERR! This is most likely a problem with the node-proxy package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! make
npm ERR! You can get their info via:
npm ERR! npm owner ls node-proxy
npm ERR! There is likely additional logging output above.
This has something to do with running node-proxy in native mode on windows instead of through cygwin.
now.js has a post detailing how to remedy this
http://blog.nowjs.com/running-nowjs-natively-on-windows
Basically you do the following:
1. Install node.exe and NPM for Windows and make sure all your paths are correct
2. Install Microsoft Visual C++ Runtime (4.8 MB)
3. Download the NowJS Windows branch zip
4. Rename `Flotype-now-XXXXXX` to `now` and put that in your node_modules folder
* note * at this point you should already have an existing “now” folder inside node_modules.
Overwrite it completely with the files from the .zip
* note * the original instructions include the following
or `git clone git://github.com/Flotype/now.git` and `git checkout windows`
This works fine but if you are new to git and using different branches, the .zip method is probably the easiest.
At this point, now.js should be available to our application =) We will confirm this a bit later on down. In order to get this app working, we will need to modify the code a bit.
I am working off the code located here:
http://nowjs.com/examples/map
Server Side Code:
We are going to configure the Server first so that it can accept connections from the client.
- Open up the app.js file inside our project. This is the main application file for node.js.
- Navigate to the URL listed above and copy all of the code from the Server section
- At the very BOTTOM of the file, paste in the server side code and save it.
If you were to try and run the server now, it will give you the following error:
C:\<path>\nodejs\nowjs-multiplayer-map-tutorial>node app.js
Express server listening on port 3000 in development mode
node.js:201
throw e; // process.nextTick error, or ‘error’ event on first tick
^
ReferenceError: nowjs is not defined
at Object.<anonymous> (C:\<path>\nowjs-multiplayer-map-tutorial\app.js:38:1)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)
So we obviously need to add some code to the server to make it work. If you go through the “how to” on node.js they define a couple of standard variables: “now” and “everyone”
We will add these in at the appropriate spots:
At the very top of the app.js, we have a list of node.js modules that are required. we will add in “now” and assign it to the variable “nowjs” as shown below:
var express = require(‘express’)
, routes = require(‘./routes’)
, nowjs = require(‘now’)
This allows us to use the “now” module.
Secondly, we want to navigate directly above the server code we pasted in from the nowjs URL and define our “everyone” variable:
// nowjs setup
var everyone = nowjs.initialize(app);
// top of the server code from http://nowjs.com/examples/map
var actors = [];
… rest of the server code
At this point, we should be able to start up the app with no errors. If you are getting errors make sure to go back and check that the “now” module for node.js was configured correctly.
Client Side Code:
Next step is to import the client code and make it available inside our project. First we are going to get the client code imported.
- Create the following file inside our project
- C:\<path>\nowjs-mutiplayer-map-tutorial\public\javascriptsfolder\map.js
- Open up the newly created Map.js file and navigate to the nowjs URL listed above and copy ALL the code inside the “Client” block and paste into the map.js file. Save
- So we have our client, but we don’t have the <canvas/> tag to hold our map, nor do we have any references to the javascript needed to run the client. This can be fixed easily by modifying the following:
- C:\<path>\nowjs-mutiplayer-map-tutorial\routes\index.js
- exports.index = function(req, res){
res.render(‘index’, { title: ‘Multiplayer Map’ })
};
- C:\<path>\nowjs-mutiplayer-map-tutorial\views\index.jade
- h1= title
canvas#map(width=500, height=400)
- C:\<path>\nowjs-mutiplayer-map-tutorial\views\layout.jade
- !!!
html
head
title= title
link(rel=’stylesheet’, href=’/stylesheets/style.css’)
script(src=’http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js’)
script(src=’/nowjs/now.js’)
script(src=’/javascripts/map.js’)
body!= body
At this point, you should be able to fire up the Server and navigate to our client URL and actually see the map!
http://localhost:3000

However! you will note that you might not be able to move around. I was unable to fully figure out why, but it has something to do with the binding of the jquery key events to the “map” canvas (?) But for now we can do one small code change and fix it for this example.
Open up the file:
C:\<path>\nowjs-mutiplayer-map-tutorial\public\javascriptsfolder\map.js
And modify the following line of code:
$(‘#map’).keydown(function(e) {
to be:
$(document).keydown(function(e) {
Refresh the client and you should see the grid scroll as you move around. Open up a second tab or browser and navigate to the same URL and you should see each dot move around independently.

I hope you learned some things from this post. If there are any errors/questions feel free to post them.
-rOck