1.1.0 • Published 4 years ago
resourceful-routes v1.1.0
Resourceful routes
A effortlessly express package to generate resourceful routes
Installation
npm install resourceful-routes
or
yarn add resourceful-routes
How to use
Import the package and pass your express router object as a param.
Example:
// routes.js
const router = require('express').Router();
require('resourceful-routes')(router, { debug: true });
router.resource('/users', UsersController, {
except: ['show', 'destroy']
});
router.resource('contacts', ContactsController, {
only: ['show'],
paramName: 'contact_id'
})
Will mount the following routes:
Route | Method |
---|---|
/users | get |
/users | post |
/users/:id | put and patch |
/contacts/:contact_id | get |
Methods
# Constructor
constructor(router, options = {})
The initial setup for the package
# constructor params
Param | Description |
---|---|
router | The express router object |
options | constructor options object |
# constructor options
Option | Type | Description |
---|---|---|
debug | Boolean | If set to true the /route/info route will be created to list all your available routes |
# Resource
resource(path, controller, options = {})
Mount all the following routes, if not specified different in options
Route | Method |
---|---|
/path | get |
/path | post |
/path/:id | get |
/path/:id | put patch |
/path/:id | destroy |
# resource params
Param | Type | Description |
---|---|---|
path | string | The path name to be mounted |
controller | object | The object containing the functions |
options | object | The options object |
# resource options
{
only: [],
except: [],
paramName: ':id'
}
Option | Type | Default | Description |
---|---|---|---|
only | Array | [] | An array containg the actions that should be mounted |
except | Array | [] | An array containg the actions that should not be mounted |
paramName | string | :id | The route param name |