Put on your helmet! Zappa is barely past the proof of concept stage and things are loosely placed. Don't use it to control your nuclear launch facility just yet. Most examples work though, and if you come with a spirit of adventure, you shall be rewarded.

(Templating is currently CoffeeKup only, but support for arbitrary engines is under way)

All your input variables are available at '@/this'. This context is also shared with your views (along with any vars you created there yourself).

The API to deal with the request and response is available in the form of locals. Those are: request/response/next (directly from express), send/redirect/session/cookies (shortcuts), params (reference to @), render (different implementation from express' response.render at the moment) and app (see bi-directional messaging below).

If you return a string, it will automatically be sent as the response.

Route combo

The routing functions accept an object where the keys are the paths for the routes, and the values are the responses. This means we can define multiple routes in one go:

get '/foo': 'bar', '/ping': 'pong', '/zig': 'zag'

Better yet:

get
'/foo': 'bar'
'/ping': 'pong'
'/zig': 'zag'

You can also use the syntax where the first param is the path, and the second the response. This is mostly to allow for regexes:

get '/foo', 'bar'
get /^\/ws-(.*)/, ->
'bloatware-' + params[0]

Bi-directional messaging (WebSockets/Comet)

But the web is not just about HTTP requests anymore. WebSockets are soon to become available on all major browsers but IE. For this sucker and legacy browsers, there's a collection of hacks that are ugly but work, and thanks to Socket.IO, we don't even have to care.

Zappa pushes this trivialization a bit further by removing some of the boilerplate, and providing some integration. The goal is to make messaging feel more like a first-class citizen along with request handling, readily available, instead of an exotic feature you bolt on your app.

All you have to do to handle bi-directional messaging in your apps is declare the handlers, side by side with your HTTP ones:

When your app starts, if you defined one of those handlers, zappa will automatically require Socket.IO and fire it up. It will not take up a dedicated port, since Socket.IO can attach itself to the HTTP server and intercept websocket/comet related messages.

Zappa uses a minimal protocol to enable handler wiring. If you send this message from the client:

{said: {text: "hi"}}

It will automatically JSON.parse it, and call the handler named said, putting the value of text in its context.

Conversely, when you call send 'welcome', online: 7, the following string will be sent to the client:

{welcome: {online: "7"}}

Message and request handlers are designed to behave as similarly as possible. The context (@/this) receives the input and is shared with templates, and there are local variables readily available to deal with the task at hand. In this case, they are: client, id, send, broadcast, render, and app.

Both types of handlers have access to the app variable, which is persistent throughout the application lifecycle. You can store temporary, app-level data here to provide some integration between your app's two "sides", like in the message counter example.

Client-side code embedding

With client you can define a route /name.js that will respond with your CoffeeScript code in JS form, and the correct content-type set. No compilation involved, since we already have you function's string representation from the runtime.

Post-rendering with server-side jQuery

Rendering things linearly is often the approach that makes more sense, but sometimes DOM manipulation can avoid loads of repetition. The best DOM libraries in the world are in javascript, and thanks to the work of Elijah Insua with jsdom, you can use some with node too.

Zappa makes it trivial to post-process your rendered templates by manipulating them with jQuery:

Splitting up

If your single file of doom is becoming unwieldy, you can split it up with whatever organization is better suited to the project(s) at hand:

include 'model.coffee'
include 'controllers/http.coffee'
include 'controllers/websockets.coffee'
include 'controllers/client.coffee'
include 'controllers/common.coffee'
include 'views.coffee'

Or by subject:

include 'users.coffee'
include 'widgets.coffee'
include 'gadgets.coffee'
include 'funzos.coffee'

Static files

If there's a ./public dir on the same level as your app's main file, static files will be automatically served from there.

Whew!

That's it for now. Big thanks to all behind the libs that are making this little experiment possible. Special thanks to Jeremy Ashkenas for CoffeeScript, the "little" language is simply amazing and incredibly flexible. To Blake Mizerany for Sinatra, the framework that made me redefine simple. To why the lucky stiff, that made me redefine hacking. And finally to Frank Zappa, for the spirit of nonconformity and experimentation that inspires me to push forward. No to mention providing the soundtrack.

"Why do you necessarily have to be wrong just because a few million people think you are?" - FZ