Sunday, September 5, 2010

Installing and Using Apps for Fab.js v0.5

‹prev | My Chain | next›

Up tonight, I would like to see if I can finish the conversion of my (fab) game to version 0.5 of fab.js. I (sort of) have faye working so the bulk of what is left is serving up static files.

Happily, Rick Olson has already created fab.static for use with fabjs v0.5 and npm-ified it. So I install it:
cstrom@whitefall:~/repos/my_fab_game$ npm install fab.static
npm it worked if it ends with ok
npm cli [ 'install', 'fab.static' ]
npm version 0.1.26
...

npm activate fab.static@0.1.0
npm build Success: fab.static@0.1.0
npm ok
Using it seems a simple matter of requiring the new module and adding a few routes:
var http = require('http'),
faye = require('faye'),
puts = require( "sys" ).puts,
inspect = require( "sys" ).inspect,
couchdb = require('node-couchdb/lib/couchdb'),
client = couchdb.createClient(5984, 'localhost'),
db = client.db('my-fab-game'),
fab = require('fab');

fab.static = require('fab.static');


//....

with ( fab )

( fab )

// Listen on the FAB port and establish the faye server
( listen, 0xFAB, attach_faye )

(route, /^\/javascript/)
(route, /^\/(.*)/)
// Stream javascript files from ./javascript
(static, "javascript", "text/javascript", "js")
()
('Not found!')
()

// Server HTML out of the html directory, relative to the root
// ("/") resource
(route, /^\/(.*)/)
// Stream HTML files from ./html
(static, "html", "text/javascript", "html")
()

()
Starting up the game backend, however, I find:
cstrom@whitefall:~/repos/my_fab_game$ ./game.js

node.js:275
throw new Error("Cannot find module '" + request + "'");
^
Error: Cannot find module 'fab.static'
at loadModule (node.js:275:15)
at require (node.js:411:14)
at Object.<anonymous> (/home/cstrom/repos/my_fab_game/game.js:17:14)
at Module._compile (node.js:461:23)
at Module._loadScriptSync (node.js:468:10)
at Module.loadSync (node.js:338:12)
at Object.runMain (node.js:521:24)
at node.js:751:10
Bah!

So is this an npm thing (e.g. npm does not work with packages that have periods)? Or is this a fab.static issue?

No matter what it is, it makes sense to check out my ~/.node_libraries directory to see what is going on there:



Hrm... No fab.static. I see fab in there, but no fab.static. So I fork it. In my local copy of fab.static, I check out the package.json file:
cstrom@whitefall:~/repos/fab.static$ cat package.json 
{ "name": "fab.static"
, "version": "0.1.0"
, "author": "technoweenie"
, "engines": ["node >= 0.2.0"]
, "main": "./lib"
}
I have always seen or used a filename (minus the extension) for the "main" attribute. I know that manynode.js things will automatically read from index.js, but perhaps this is not one of them.

So I change "main" to include the full module ID:
cstrom@whitefall:~/repos/fab.static$ cat package.json 
{ "name": "fab.static"
, "version": "0.1.0"
, "author": "technoweenie"
, "engines": ["node >= 0.2.0"]
, "main": "./lib/index"
}
I install my local copy:
cstrom@whitefall:~/repos/fab.static$ npm install .
npm it worked if it ends with ok
npm cli [ 'install', '.' ]
npm version 0.1.26
...

npm activate fab.static@0.1.0
npm build Success: fab.static@0.1.0
npm ok
That seems to have worked, but then again so did the first install. The difference is that fab.static is now in my ~/node_libraries:



Reading through npm-json, I almost think it ought to work with "main": "./lib":
In fact, "main" is just sugar around setting a module named "index".
Clearly, it does not quite work like that. Ah well, I know that it definitely works with "main": "./lib/index".

Best of all, the game is now fully functional again. There may just be something to this new, extreme modular fabjs after all! Aside from a minor packaging issue, I was able to install and use a fab app very quickly tonight.

There is still the dashboard that is not quite working, but that gives me an excuse to write a (fab) app of my own. Tomorrow.

Day #217

No comments:

Post a Comment