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:
- Return a response
- Throw an error
- Pass controll and data to the next handler down the pipeline
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
Next Callback
A callback function passed into each handler. Its used for passing data to next handler, invoke another controller
instance, or re-route the flow to another controler instance.
- next(data)
-
data
{any}
Data that will be passed to the next handler
- next(controller, [data], [routingOptions])
-
controller
{function} controllerInstance
Another controller instancedata
{any}
- Data that will be passed to the next handler. OptionalroutingOptions
{object} routingOptions
- routingOptions object. Optional
- routingOptions
-
reroute
{boolean}
- Indicates how the redirect should be handled. Setting it tofalse
will run the handlers declared in the provided controller as a part of the current pipeline. This means that all the errors will be handled by our current error handler and if we have additional handlers below, the flow will resume after all the handlers from provided controller will be passed. On the other hand, setting this parameter totrue
will completely re-route the flow to the provided controller, ignoring everithing that is declared below in the current pipeline. Defaults tofalse
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