npm install headrest
Assumes you init your JS app with or otherwise set an api root URL from which your models build the correct URL for their resource.
You can set the apiRoot like:
headrest = require'headrest'apiRoot: '/apis/'dbPath: '/tmp/gruntest.json'# headrest is an express appheadrestlisten5001
It assumes your resources have paths like:
/api/rocksor nested as deep as you like -
/api/rocks/ffeedc/sedimentariesand so on.
/api/rocks/ffeedc, also nested as far as you want.
Implements sessions at
POST whatever you want to
/api/session, you'll get a Cookie.
/api/session - HTTP 200 means you're logged in, 404 if not.
/api/session - removes your session record and sets your cookie
headrest = require'headrest'apiRoot: '/apis/'dbPath: '/tmp/gruntest.json'=gruntloadNpmTasks'grunt-contrib-connect'gruntregisterTask'headrest''connect'gruntinitConfigconnect:headrest:options:port: 5001hostname: "0.0.0.0"keepalive: true:headrest
This will start the server on
http://0.0.0.0:5001/. From a browser, you
can create an object with:
var request = $ajaxurl: ""dataType: "json"crossDomain: truetype: 'POST'data: name: "sedimentary"requestdonevar rock = JSONparsedconsole.log rockid// console output:// > 7cf28fd544be1b7f55296c45d40538fvar request = $ajaxurl: ""dataType: "json"crossDomain: truetype: 'PUT'data: name: "Igneous"requestdoneconsole.log'updated'var request = $ajaxurl: ""dataType: "json"crossDomain: truetype: 'DELETE'requestdoneconsole.log'removed'
Or if any of this code is wack and you think there's a better way to do it, I definitely want to hear about it, no joke.
I yanked this idea from my homie Mark's footrest.py