Getting Started with Restful

Restful aims at doing more than just providing a router with a callback. It aims to provide developer happiness, productivity, and testability. It does things the 'Unix' way by providing several small functions that can be composed together.

By leveraging functional programming over OO, we end up with a system that is very easy to test. Every function takes an input, the connection object and returns a value, again the connection object.

The middleware, router, controller, and optional view (serialization) layers form a composable pipeline with each layer each taking a conn, transforming it, and passing it down to another layer until it's finally sent to the client.

You can think of the 'functional' pipeline conceptually like this:

Request - localhost/posts
               V
               V
Middleware (transforms all connections)
               V
               V
Router - (Dispatches matching route to controller)
               V
               V        
Controller (side effects and data mutation)
               V
               V        
View (transform payload eg, wrapping with 'data', omitting fields, or conv. timestamps)
               V
               V            
Connection.req is terminated and sent to client

There are no hard rules with Restful but it you follow these you'll have a more consistant time:

  • Router goes in server/routes.js
  • Use ES6 de-structuring to make code more readable
  • All resource namespaces are singular to prevent confusion, eg (PostController)
  • Use the module pattern (see guide) eg Models.Post in server/models/post.js
  • Controllers goes in server/controllers/resource_name.js
  • Views (if used) go in server/views/resource_name.js