modella-resource
v0.13.0
Published
Expose Modella models via RESTful resource middleware.
Downloads
44
Readme
modella-resource
Expose Modella models via Express middleware. Adds REST routes with callbacks including self-describing OPTIONS response for each route.
This module can be paired with modella-ajax for automatic client-server communication.
Installation
npm install modella-resource
Example
Pass a Modella model constructor to the modella-resource middleware and mount it:
var app = express();
, modella = require('modella');
, resource = require('modella-resource');
var User = modella('User');
app.use(resource(User).middleware());
These routes will then be available:
/users
GET, POST, OPTIONS
/users/count
GET, OPTIONS
/users/:id
GET, PUT, DELETE, OPTIONS
Self-describing OPTIONS
If an OPTIONS
request is made to any endpoint defined by modella-resource, a
JSON description of the available actions is included in the response body.
You can combine this with OPTIONS middleware mounted at your API root path, which responds with a JSON description of the available resources.
Actions
You can override the resource actions if you want to customize the route
callbacks. Each actions is called with arguments Model, req, res, next
:
app.use(resource(User, {
index: function(Model, req, res, next) { },
count: function(Model, req, res, next) { },
show: function(Model, req, res, next) { },
create: function(Model, req, res, next) { },
update: function(Model, req, res, next) { },
destroy: function(Model, req, res, next) { }
});
Nesting resources
You can nest resources using resource.add()
:
var UserResource = resource(User);
var PostResource = resource(Post);
app.use(UserResource.add(PostResource).middleware());
This creates routes such as /users/:id/posts
and so on.
Resource#middleware
Returns Express/Connect middleware.