3 ms·
Warning: shameless self-promotion ahead: db = require('./your_sexy_data_access_module') server = require('lazorse').server port: 3000, -> # in this
by grncdr 14y ago
Warning: shameless self-promotion ahead:
db = require('./your_sexy_data_access_module')
server = require('lazorse').server port: 3000, ->
# in this context we register resources the app using @resource
@resource '/things/{thingId}':
shortName: "Things"
GET: ->
db.getThing @thingId, @data
POST: ->
db.getThing @thingId, (err, thing) =>
return @next(err) if err
thing.update(@req, @data)
Where @data is node-style callback function taking (err, data), and the data will be rendered by a separate middleware layer that inspects Accept headers and does the Right Thing.
There's a lot more than should be covered in an HN comment, but in interest of DRYing out the above example, let's rewrite it to use a coercion to retrieve "thing" instances.
db = require('./your_sexy_data_access_module')
server = require('lazorse').server port: 3000, ->
@coerce 'thing', "Things are whatever this app deals in", db.getThing
@resource '/things/{thing}':
shortName: "Things"
GET: ->
# Use @ok because we won't get here if there was an error retrieving
# the thing
@ok @thing
POST: ->
@thing.update @req, @data
See http://betsmartmedia.github.com/Lazorse http://betsmartmedia.github.com/Lazorse for full API docs and examples. Bug reports, pull requests, flame comments etc. are all very welcome :)