Skip to content

Instantly share code, notes, and snippets.

@joshmcarthur
Created February 8, 2012 08:21
Show Gist options
  • Save joshmcarthur/1766753 to your computer and use it in GitHub Desktop.
Save joshmcarthur/1766753 to your computer and use it in GitHub Desktop.
GET /deals/:latitude/:longitude
# Create a GET route, with named route segments for the latitude and longitude
app.get '/deals/:latitude/:longitude', (req, resp) ->
# Set the response content type to JSON
resp.contentType("application/json")
# Build the URI for retrieving deals from TreatMe
api_options = {
host: 'treatme.co.nz',
port: 80,
path: "/now/GetCategorizedDeals?latitude=#{req.params['latitude']}&longitude=#{req.params['longitude']}"
}
try
# Make the HTTP request - the callback will be executed when the
# response starts to come in, and by listening for the 'data' and 'end'
# events, we can build up the JSON we want to send back to the front-end
http.get api_options, (res) ->
res.on 'error', ->
resp.send(error_json(), 500)
json = ""
res.on 'data', (chunk) ->
json += chunk
res.on 'end', ->
resp.send(json)
catch error
# Respond with an error message if anything went wrong
resp.send(error_json(), 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment