Router
In a SoapJS project, the Router
plays a crucial role in managing the application's routes and integrating them with the chosen web framework. The router is automatically generated when a new project is initiated using the soap new project
command, but this only occurs if the user opts to include a web framework during the setup process.
Key Features of the Router:
Automatic Integration: When you add new routes to your application using the CLI, the router is automatically updated to include this new route. This seamless integration ensures that all your routes are correctly registered and handled by the application's server.
Framework Compatibility: The SoapJS router is designed to work with most common web frameworks as long as they support standard request methods such as
get()
,post()
,delete()
, etc. This flexibility allows you to integrate the router with your preferred framework without significant changes to the routing logic.Configuration: The name and location of the router file can be customized through the
plugin.config.json
. This allows for a personalized project structure that fits your application's specific needs.
Example Router Code:
In this example, the Router
class extends from Soap.Router
, providing a structured way to configure routes. The configure
method is where the routes are set up:
Dependency Injection: The router utilizes dependency injection to obtain instances of controllers, in this case,
CustomerController
. This pattern decouples the route handling logic from the controllers, promoting cleaner code and easier testing.Route Registration: Routes are registered using the
mount
method. Here,GetCustomerDetailsRoute.create(customerController.getCustomerDetails)
dynamically creates a route and links it to thegetCustomerDetails
method inCustomerController
.
By following this structure, SoapJS facilitates a streamlined approach to route management, ensuring that your application's navigation and data flow are efficiently handled.
Remember, this router setup is part of the broader SoapJS ecosystem, designed to enhance your application's architecture while maintaining flexibility and compatibility with various web frameworks.
Last updated