Skip to content

Instantly share code, notes, and snippets.

@mauricemach
Created September 9, 2011 14:02
Show Gist options
  • Save mauricemach/1206296 to your computer and use it in GitHub Desktop.
Save mauricemach/1206296 to your computer and use it in GitHub Desktop.
zappa =
run: (f)->
# Re-compile the function in a context that contains the Zappa
# methods as globals.
app = require('express').createServer()
vm = require 'vm'
code = 'zappa_code='+f.toString()
views = {}
router = {}
context =
view: (name,code) ->
views[name] = code
render: (name) ->
# ReferenceError: this_object is not defined
views[name].apply(this_object)
get: (location,code) ->
router[location] = code
try
vm.runInNewContext code, context, 'zappa.run() in bob.coffee'
catch error
console.log "Error = #{error}"
context.zappa_code()
# The following code gets called from the httpServer's router.
app.get '/', (req, res) ->
this_object = # Populated from request, etc.
params: req.query
request: req
response: res
router['/'].apply(this_object)
app.listen 3000
zappa.run ->
get '/', ->
render 'bob'
view 'bob', ->
"Hello bob, #{@params.foo}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment