Skip to content

Latest commit

 

History

History
132 lines (87 loc) · 3.91 KB

index.md

File metadata and controls

132 lines (87 loc) · 3.91 KB
layout title
default
CoffeeScript-optimized interface for building web apps on Node.js with Express and Socket.IO.

Node development for the lazy

If you can describe it in 495 characters, why on earth should it take 879?

Zappa is a CoffeeScript-optimized interface to Express and Socket.IO that makes this:

{% highlight coffeescript %} require('zappa') -> Gizmo = require './model/gizmo'

@use 'bodyParser', 'methodOverride', @app.router, 'static'

@configure development: => @use errorHandler: {dumpExceptions: on} production: => @use 'errorHandler'

@get '/': -> @render 'index'

@get '/gizmos/:id': -> Gizmo.findById @params.id, (err, gizmo) => @render index: {err, gizmo}

@on connection: -> @emit welcome: {time: new Date()}

@on shout: -> @broadcast shout: {@id, text: @data.text} {% endhighlight %}

Equivalent to this:

{% highlight coffeescript %} express = require 'express' app = express.createServer() io = require('socket.io').listen(app)

Gizmo = require './model/gizmo'

app.use express.bodyParser() app.use express.methodOverride() app.use app.router app.use express.static __dirname + '/public'

app.configure 'development', -> app.use express.errorHandler dumpExceptions: on

app.configure 'production', -> app.use express.errorHandler()

app.get '/', (req, res) -> res.render 'index'

app.get '/gizmos/:id', (req, res) -> Gizmo.findById req.params.id, (err, gizmo) -> res.render 'index', {err, gizmo}

io.sockets.on 'connection', (socket) -> socket.emit 'welcome', time: new Date()

socket.on 'shout', (data) -> socket.broadcast.emit 'shout', id: socket.id, text: data.text

app.listen 3000

console.log "Express server listening on port %d in %s mode", app.address().port, app.settings.env {% endhighlight %}

And throws in some additional features while at it:

{% highlight coffeescript %} require('zappa') -> @enable 'default layout', 'serve jquery', 'serve sammy', 'minify'

@get '/': -> @render 'index'

@on connection: -> @emit welcome: {result: sum 1, 2}

@shared '/shared.js': -> root = window ? global root.sum = (x, y) -> x + y

@client '/index.js': -> @connect()

@get '#/route': ->
  $('body').append 'client routes!'

@on welcome: ->
  $('body').append "welcomed: #{sum @data.result, 2}"

@view index: -> @title = 'PicoChat!' @scripts = ['/socket.io/socket.io', '/zappa/jquery', '/zappa/sammy', '/zappa/zappa', '/shared', '/index']

h1 @title

{% endhighlight %}

Learn More

Other resources

  • The source code repository at github

  • Questions, suggestions? Drop us a line on the mailing list

  • Rather do it realtime? Join the IRC channel on freenode: [#zappajs]((irc://irc.freenode.net/zappajs)

  • Found a bug? Open an issue at github

  • Check the project's history at the change log

  • Migrating from an earlier version? Read the announcements (0.2.x/0.3.x) for an overview on changes, and follow the TL;DR migration guides (0.2.x/0.3.x)

  • Deploying to heroku? Check this blog post