2 controllers - devlinjunker/template.hapi.rest GitHub Wiki

Controllers

Controllers are functions that handle requests to specific endpoint paths:

function controllerFunction(request) {
    ... do stuff and return a message ...
}

The first parameter passed in is the [HapiRequest](https://github.com/hapijs/hapi/blob/master/API.md#request for HapiRequest) object

We define the mapping from endpoint/method to controller function with an JSON object:

const map1 = {
  path: "/endpoint",
  method: "GET", ///
  controller: controllerFunction
}

To make this endpoint available to the server, we need to export an array of these mappings:

export default [ map1, map2, ... ];

Notes/Ideas

  • OPTIONS requests?
  • Do not instantiate classes to handle requests. This would be a huge memory overhead!
  • DO NOT USE SHARED CLASS PROPERTIES/STATEFUL VARIABLES IN CONTROLLER FUNCTIONS
    • These functions need to be stateless.
  • IDEA: Catch any exceptions that don't have response code and log or email indicating unexpected state
  • IDEA: generic CRUD endpoints with a flexible storage system for prototypes
    • /{objectType}/create
    • /{objectType}/read/{id}
    • /{objectType}/update/{id}
    • /{objectType}/delete/{id}
    • /{objectType}/all?filters?fields?
    • /{objectType}/search?q