Skip to content
reflect RESTful Director routers from Resourceful resources
Find file
New pull request
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Build Status

Creates RESTful Director routers for resourceful resources. Can be used as a stand-alone module or as a Flatiron plugin.


The restful project removes the process of writing boilerplate routing code for interacting with resourceful resources. Restful uses reflection to reflect an http router interface which maps all the restful routes needed to perform basic CRUD operations with resourceful. restful also has the ability to expose additional arbitrary remote resource methods through it's http router interface. Through the removal of this boilerplate code, restful creates a robust, standardized, and re-usable http interface for any resourceful resource.


 npm install restful


As a Flatiron Plugin

To use restful as a Flatiron plugin you will have to:

  • Define a resource in your Flatiron app
  • Include the restful plugin in your Flatiron app
  • Set the resource as restful in order to tell Flatiron to expose it

Here is a code example of using restful as a Flatiron plugin:

As a stand-alone server

To use restful as a stand-alone server you will have to:

  • Define resourceful resource(s)
  • Create a new restful router based on the resource(s)
  • Use the newly created router inside an http server

Here is a code example of using restful as a stand-alone server:

Core HTTP REST Mappings

By default, restful will map the following Resourceful methods.

Verb    Path                    Action                 Notes

GET     /creatures           => Creature.all()
POST    /creatures           => Creature.create()      Create with no-id, id is auto-generated
POST    /creatures/1         => Creature.create()      Create with id "1"
GET     /creatures/1         =>
PUT     /creatures/1         => Creature.update()
DELETE  /creatures/1         => Creature.destroy()
POST    /creatures/1/update  => Creature.update()
POST    /creatures/1/destroy => Creature.destroy()

The Director router will dispatch all incoming RESTFul urls to the Creature resource and respond back with the appropriate result.

Non-strict Mappings

You'll notice that some of the routes defined above are not 100% restful ( such as POST /creatures/1/update ).

Since not all HTTP clients support PUT and DELETE Verbs ( such as forms in web browsers ), restful maps additional "non-strict" rest mappings to make your life slightly easier.

If you prefer to not use this option, set { strict: true }. You might also want to consider using a rails-like approach which uses the convention of a reserved <form> input field called "_method" which contains either "PUT" or "DELETE" see:

Relational Resources

To define relational data in restful you will have to:

  • Define the relationship in the resource itself using the resourceful Resource.parent() API
  • Create a new router based on the resource(s)

restful will then properly reflect the relational properties of your resources into the routing layer.

Here is a simple code example of using restful with Albums and Songs:

Protip: You'll want to browse the restful server using the HTML API explorer in order to see which routes will be created.

Built-in HTML API Explorer

restful comes built in with a built-in HTML API explorer. If you wish to view the current routing map for the router as HTML, simply start the restful server and visit http://localhost:8000/ in your browser. If you prefer to not use this option, set { explore: false }.

Note: The API explorer is powered by director-explorer, which will work for ANY director router.

Exposing Arbitrary Resource Methods

In many cases, you'll want to expose additional methods on a Resource through the router outside of the included CRUD operations: create, all, show, update, destroy.

restful has built in support for easily exposing arbitrary remote resource methods.

Consider the example of a Creature. We've already defined all the restful CRUD routes, but a Creature also needs to eat!

Simply create a new method on the Creature resource called feed.

Creature.feed = function (_id, options, callback) {
  callback(null, 'I have been fed');

This feed method is consider private by default, in that it will not be exposed to the web unless it's set to a remote function. To set a resource method to remote, simply:

Creature.feed.remote = true

It's easy as that! By setting the feed method to remote, the following routes will exist in the Director router.

POST    /creatures/1/feed    => Creature.feed()
GET     /creatures/1/feed    => Creature.feed()

Resource Security

There are several ways to provide security and authorization for accessing resource methods exposed with restful. The recommended pattern for authorization is to use resourceful's ability for before and after hooks. In these hooks, you can add additional business logic to restrict access to the resource's methods.

It is not recommended to place authorization logic in the routing layer, as in an ideal world the router will be a reflected interface of the resource. In theory, the security of the router itself should be somewhat irrelevant since the resource could have multiple reflected interfaces that all required the same business logic.

TL;DR; For security and authorization, you should use resourceful's before and after hooks.


 npm test


  • Full resourceful property type support ( numeric, boolean, array, object )
  • Full resourceful nested property schema support
  • Add ability to specify schemas for remote method argument payloads
  • Implement and document browser support
  • Improve Tests
  • Add better error support via errs library
Something went wrong with that request. Please try again.