Skip to content

Instantly share code, notes, and snippets.

Created June 4, 2011 17:13
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bohde/1008081 to your computer and use it in GitHub Desktop.
Simple GET Param to CouchDB app
_ = require 'underscore'
express = require 'express'
cradle = require 'cradle'
class Queue
# Class to store the queue documents to store in Couch.
constructor: ->
_.bindAll @, 'flush'
@reqs = []
@db = new(cradle.Connection)().database('analytics')
setInterval @flush, 1000
# If a value is a string representation of a float,
# let's convert it.
transform: (query) ->
ret = {}
for name, value of query
ret[name] = parseFloat(value) or value
# Add a request to be flushed
push: (req) ->
serialized = _.extend {}, @transform(req.query),
timestamp: new Date()
'user-agent': req.headers['user-agent']
'referer': req.headers['referer']
ip: req.connection.remoteAddress
@reqs.push serialized
# Save pending requests to Couch,
# outputting to the console so it can be recovered
# in case something bad happens
flush: ->
if @reqs and @reqs.length
console.log @reqs @reqs
@reqs = []
queue = new Queue()
app = express.createServer()
app.get '/tracking.gif', (req, res) ->
res.header 'Cache-Control', 'private, no-cache, proxy-revalidate'
app.listen 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment