1.1.0 • Published 4 years ago

resourceful-routes v1.1.0

Weekly downloads
12
License
MIT
Repository
github
Last release
4 years ago

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:

RouteMethod
/usersget
/userspost
/users/:idput and patch
/contacts/:contact_idget

Methods

# Constructor

constructor(router, options = {})

The initial setup for the package

# constructor params

ParamDescription
routerThe express router object
optionsconstructor options object

# constructor options

OptionTypeDescription
debugBooleanIf 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

RouteMethod
/pathget
/pathpost
/path/:idget
/path/:idput patch
/path/:iddestroy

# resource params

ParamTypeDescription
pathstringThe path name to be mounted
controllerobjectThe object containing the functions
optionsobjectThe options object

# resource options

{
  only: [],
  except: [],
  paramName: ':id'
}
OptionTypeDefaultDescription
onlyArray[]An array containg the actions that should be mounted
exceptArray[]An array containg the actions that should not be mounted
paramNamestring:idThe route param name