Easily route a resource's crud methods to express apps!
Example
Here's a User resource that exposes some CRUD methods:
moduleexports = { ... ; //first argument is response status, second is array of response data } {} {} {} {};
Here's how you add the routes to your express app:
var app = ;var User = ; app; app;
Here's how you add the routes to your express app using a formatted response object:
var app = ;var User = ;var opts = { return timestamp: Date payload: result ; }; app opts; app;
Now your express app has the following routes:
DELETE /users/:id
GET /users
GET /users/:id
POST /users
PUT /users/:id
Changing the param name
You can change the param name by specifying it at the end of your route:
app;
Now your express app has the following routes:
DELETE /users/:userId
GET /users
GET /users/:userId
POST /users
PUT /users/:userId
With some middleware
var {//arity matters! ifreqqueryusername !== 'foo'return ; ;};var {;}; app;
Restrict access to resources by user
app;
Format your response
With any path
app;
Status Codes
When you call the callback given to resource methods without an error object as the
first arg, express-crud
will use either 204
or 200
depending on the context.
If you wish to pass a non 2xx
status code up to your application the preferred
way is to assign a status
property to your error
object I.E. cb({status: 404})
.