Controller Constructor Function

Controller Constructor is a constructor wrapper function, it returns a new controller instance.
controller([...params])
...params {function} handler | controllerInstance
Accepts any number of handlers or controller instances. Returns a new Controller Instance
Inheritance
A controller instance can inherit handlers from another controller by passing it as input to the constructor. The constructor accepts an unlimited number of controller intances or handlers functions. The resulting controller will inherit all the handlers from controllers as well as handlers passed as input to the constructor, in the same order as declared in the constructor.
Example
Methods

controller.setDefaultErrorHandler(errorHandler)
errorHandler {function} errorHandler
errorHandler function
Sets a global error handler per whole application.
Example

Controller Instance

A Controller Instance is a function with an object-like structure. It is generated by the controller constructor and its purpose is to combine multiple handlers into a pipeline in which the output of each handler serves as the input to its successor handler.
Creating a controller instance
Methods

controllerInstance.beforeAll(handler)
handler {function} handlerFunction | constrollerInstance
Accepts a handler function or a controller instance.
Inserts a handler function at the beginning of the pipeline, similar to unshift() in array. It can accept another controller instance. In case a controller instance is passed, all its handlers will be added to the current pipeline keeping same order as they where defined in the original controller, at the beginning of the current pipeline.
Example

controllerInstance.do(handler)
handler {function} handlerFunction | constrollerInstance
Accepts a handler function or a controller instance.
Inserts a handler function at the end of the pipeline, similar to pus() in array. It can accept another controller instance. In case a controller instance is passed, all its handlers will be added to the current pipeline keeping same order as they where defined in the original controller, at the end of the current pipeline.
Example

controllerInstance.end(endHandler)
endHandler {function} endHandler
End Handler function
Inserts a endHandler. It's defined once per controller, and gets called after all the handlers in the pipeline have passed successfilly
Example

controllerInstance.catch(errorHandler)
errorHandler {function} errorHandler
Error Handler function
Inserts a errorHandler. It's used to chatch errors in the pipeline. If a controller is missing an errorHandler, errors will end up in the globalErrorHandler
Example

controllerInstance.promise(req, res, [data])
request {object} Request Object
Request object, depending on the framework used.
response {object} Response Object
Response object, depending on the framework used.
data {any} optional
Any data served as input to the first handler in the pipeline
returns: Promise
Transforms the pipeline into a promise. Invoking this method will ignore .end() and .catch() the resulting value of the promise will be the value passed in next(...) by the last handler in the pipeline
Note that each framework handles the request-response cycle differently. Express.js uses request/response objects while Hapi.js uses request/h. Either way our controller instance is just passing those parameters to handler functions without interacting.
Example

controllerInstance.middleware(req, res, data)
request {object} Request Object
Request object, depending on the framework used.
response {object} Response Object
Response object, depending on the framework used.
data {any} optional
Any data served as input to the first handler in the pipeline
returns: ExpressMiddleware
Transforms the pipeline into Express.js middleware. Invoking this method will ignore .end() and .catch(). Note that Express does not use next() to pass data, therefore any data passed by the last handler through next(...) will be ignored.
Example

Running the pipeline
controllerInstance(req, res, data)
request {object} Request Object
Request object, depending on the framework used.
response {object} Response Object
Response object, depending on the framework used.
data {any} optional
Any data served as input to the first handler in the pipeline
Note that each framework handles the request-response cycle differently. Express.js uses request/response objects while Hapi.js uses request/h. Either way our controller instance is just passing those parameters to handler functions without interacting.
Example

Handler Function

A handler is a function responsible for a specific task in a chain of consecutive handlers, where the output of each handler serves as input to the succeding handler. A handler function can:
  1. Return a response
  2. Throw an error
  3. Pass controll and data to the next handler down the pipeline
Similar to Express's middleware, it uses next() to pass controll to the next handler, but with an essential difference: instead of passing an error, we use next() to pass data to the next handler, while having a separate callback for errors.
Params
request {object} Request Object
Request object, depending on the framework used.
response {object} Response Object
Response object, depending on the framework used.
next {function} nextCallback
Next Callback
errorCb {function} errorCallback
Error Callback
data {any}
Any input data
Example

Error Handler Function

Is a function responsible for catching and handling errors passed by an error callback.
Params
request {object} Request Object
Request object, depending on the framework used.
response {object} Response Object
Response object, depending on the framework used.
Error {any}
Any data which represents the error
Example

End Handler Function

Is a function that get's call at the end of a pipeline. Its purpose is to prepare and submit a response. Since it's the last handler in the call-chain, it lacks a next() callback.
Params
request {object} Request Object
Request object, depending on the framework used.
response {object} Response Object
Response object, depending on the framework used.
errorCb {function} errorCallback
Error Callback
data {any}
Any data passed by the last handler in the pipeline
Example


Error Callback

A callback function used for reporting errors. The reported error will end up in the controller's error handler if such is declared. Otherwise it will end un in the global erorr handler.
errorCb(error)

error {any} Any data structure that represents an error

Example