koa-resource-router
RESTful resource routing for koa.
- Rails-like REST resource routing.
- Use multiple middleware for resource actions.
- Responds to
OPTIONS
requests with allowed methods. - Returns
405 Method Not Allowed
when applicable.
Installation
Install using npm:
npm install koa-resource-router
API
new Resource(path, actions, options)
var Resource = ;var app = ; var users = 'users' // GET /users { } // GET /users/new { } // POST /users { } // GET /users/:id { } // GET /users/:id/edit { } // PUT /users/:id { } // DELETE /users/:id { }; app;
Action mapping
Actions are then mapped accordingly:
GET /users -> indexGET /users/new -> newPOST /users -> createGET /users/:user -> showGET /users/:user/edit -> editPUT /users/:user -> updateDELETE /users/:user -> destroy
Overriding action mapping
var users = 'users' actions methods: update: 'PATCH' ;
Top-level resource
Omit the resource name to specify a top-level resource:
var root = ;
Top-level controller actions are mapped as follows:
GET / -> indexGET /new -> newPOST / -> createGET /:id -> showGET /:id/edit -> editPUT /:id -> updateDELETE /:id -> destroy
Nesting
Resources can be nested using resource.add()
:
var forums = 'forums' ;var threads = 'threads' ; forums;
Multiple middleware
Run middleware before resource actions by passing middleware functions before your actions:
var users = 'users' authorize actions;
Run middleware for specific actions by passing an array:
var users = 'users' show: authorize { // ... };