S42-Core Controller
CONTROLLER
Overview
The Controller
class/module in s42-core
provides a way to define and manage HTTP endpoints and their associated methods and middlewares. It is designed to be flexible and allows the attachment of various functionalities to each endpoint.
Important Note
While the Controller
class is a useful utility, it is not mandatory to use it with s42-core
. The goal of s42-core
is to be agnostic to any framework, providing the flexibility to integrate with any other system or framework you prefer.
Methods
getMethods()
Returns the list of HTTP methods associated with the controller.
setPath(path: string)
Sets the path for the controller.
- path: The path to set for the controller.
- returns: The controller instance.
getPath()
Returns the path set for the controller.
update()
Adds the UPDATE
HTTP method to the controller.
- returns: The controller instance.
patch()
Adds the PATCH
HTTP method to the controller.
- returns: The controller instance.
options()
Adds the OPTIONS
HTTP method to the controller.
- returns: The controller instance.
get()
Adds the GET
HTTP method to the controller.
- returns: The controller instance.
delete()
Adds the DELETE
HTTP method to the controller.
- returns: The controller instance.
post()
Adds the POST
HTTP method to the controller.
- returns: The controller instance.
put()
Adds the PUT
HTTP method to the controller.
- returns: The controller instance.
use(callback: (req: any, res: any, next?: Middleware) => void)
Adds a middleware function to the controller. This method allows attaching various functionalities to the endpoint, but only one of these middlewares should send the final response to the client.
- callback: The middleware function to add.
- returns: The controller instance.
getCallback(): (req: any, res: any) => void
Returns a callback function that processes the middlewares and sends the final response to the client.
Use Cases
Example 1: Setting Up a Controller with Multiple HTTP Methods
You can define a controller with multiple HTTP methods to handle different types of requests for a specific endpoint.
Example 2: Adding Middlewares
Attach various middleware functions to a controller to handle tasks such as authentication, validation, logging, etc. Remember, only one middleware should send the final response.
#Integrating with Other Frameworks
Since s42-core
is agnostic to any framework, you can use the Controller
class to manage your endpoints and integrate it seamlessly with any other framework or system you are using.
By utilizing the Controller
class, you can define and manage your HTTP endpoints efficiently, leveraging the flexibility and power of s42-core
to build robust and scalable applications.