Expose Modella models via RESTful resource middleware.
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.
npm install modella-resource
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';appuseresourceUsermiddleware;
These routes will then be available:
/usersGET, POST, OPTIONS/users/countGET, OPTIONS/users/:idGET, PUT, DELETE, OPTIONS
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.
You can override the resource actions if you want to customize the route
callbacks. Each actions is called with arguments
Model, req, res, next:
You can nest resources using
var UserResource = resourceUser;var PostResource = resourcePost;appuseUserResourceaddPostResourcemiddleware;
This creates routes such as
/users/:id/posts and so on.
Returns Express/Connect middleware.