Wednesday, August 25, 2010

Express.js

‹prev | My Chain | next›

At the risk of straying to far on the path of my chain, tonight I would like to play around a bit with express. With the switch to faye, the fab.js backend in my (fab) game is doing little more than serving up static HTML and CSS. If express is as easy as advertised, I ought to be able to swap backends quickly.

First up, I install express via npm:
cstrom@whitefall:~/repos/my_fab_game$ npm install express
npm it worked if it ends with ok
npm cli [ 'install', 'express' ]
npm version 0.1.26
npm config file /home/cstrom/.npmrc
npm config file /home/cstrom/local/etc/npmrc
npm install pkg express
...

npm activate express@1.0.0rc2
npm activate connect@0.2.4
npm build Success: express@1.0.0rc2
npm build Success: connect@0.2.4
npm ok
With express installed, I remove my fab.js code replacing it with express.js code. Since the HTML and CSS is all static, the only "real" work I do is use the built-in express static provider:
var express = require('express'),
http = require('http'),
faye = require('faye'),
puts = require( "sys" ).puts;

// Create the Express server
var app = express.createServer();

// Serve statics from ./public
app.use(express.staticProvider(__dirname + '/public'));


// Server-side extension to lock player messages to client that added
// the player in the first place,
// http://japhr.blogspot.com/2010/08/per-message-authorization-in-faye.html
var serverAuth = {
incoming: function(message, callback) {
// ...
}
};

// Faye nodejs adapter
var bayeux = new faye.NodeAdapter({
mount: '/faye',
timeout: 45
});

// Add the server-side faye extension
bayeux.addExtension(serverAuth);

// Add the faye nodejs faye adapter to the nodejs server
bayeux.attach(app);

// Listen on port 3000
app.listen(3000);
The faye code came directly from the fab.js version of the app, everything else is basic express.js.

And it just works. Me like.


Day #206

No comments:

Post a Comment