Mixin: webapi

webapi

This mixin provides webapi() and webmethods() and method to implement a RESTful Web API feature for Express.js.
Source:

Example

// This emulates how webapi() works with express to provide RESTful Web APIs.

var express = require('express');
var KagoDB = require('KagoDB');

var collection = new KagoDB({ storage: 'memory' });
var webapi = collection.webapi();
var webmethods = collection.webmethods(); // == webapi.methods

var app = express();
app.use(webapi.bodyParser());
app.use(webapi.prepare());
app.use(webapi.ready());
app.put('/data/:id?', webmethods.write);
app.del('/data/:id?', webmethods.erase);
app.all('/data/:id?', webapi.dispatch(webmethods));
app.head('/data/:id?', webmethods.read);
app.get('/data/:id?', webmethods.read);
app.use(webapi.cleanup());
app.use(webapi.error(400));
app.listen(3000);