Controllers
Controllers are classes that handle routes request.
In order for the router to be able to handle the request, a controller class must extend Controller
.
Each controller action will be called with IRequest
and IResponse
objects.
controller action can return a promise, or an object that will be passed to res.send(someData)
with status code 200
#
Request ControllerBy default, appolo
creates a new controller instance for every request.
#
Static ControllersIf you do not need a new controller instance for every request, you can inherit from StaticController
which is singleton and created once.
#
Methodssupported http methods
@get
@post
@patch
@patch
@put
@del
@purge
multi methods and paths on the same action also supported
#
API#
send#
send(statusCode?: number, data?: any)send response with status and optional data
#
sendOk#
sendOk(data?: any)send response with status 200 optional data
#
sendCreated#
sendCreated(data?: any)send response with status 201 optional data
#
sendNoContent#
sendNoContent()send response with status 204
#
sendError#
sendError(error?: Error | string, code?: number, data?: any)send response error with status 500
#
sendBadRequest#
sendBadRequest(error?: Error | string, code?: number, data?: any)send response error BadRequestError
with status 400
#
sendUnauthorized#
sendUnauthorized(error?: Error | string, code?: number, data?: any)send response error UnauthorizedError
with status 401
#
sendNotFound#
sendNotFound(error?: Error | string, code?: number, data?: any)send response error NotFoundError
with status 404
#
getModel#
getModel(): Treturn combined object of req.query
, req.body
and req.params