Middleware
Middleware serves as a connector between routes and controllers, and its significance lies in bolstering the security, scalability, and adaptability of RESTful services.
Class based middleware
dox create:middleware Logger
class LoggerMiddleware extends IDoxMiddleware {
@override
handle(IDoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next to the controller
return req;
}
}
var loggerMiddleware = LoggerMiddleware();
Route.get('ping', [loggerMiddleware, webController.pong]);
Function based middleware
If you prefer not to use class-based middleware, you have the option to create middleware functions and apply them to the route.
authMiddleware(DoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next to the controller
return req;
}
Route.get('ping', [ authMiddleware, webController.pong ]);
Route with multi-middleware
class LoggerMiddleware extends IDoxMiddleware {
@override
handle(IDoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next to the controller
return req;
}
}
authMiddleware(DoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next to the controller
return req;
}
class WebController {
pong(DoxRequest req) async {
return 'pong';
}
}
Route.get('ping', [ loggerMiddleware, authMiddleware, webController.pong ]);
Info
You can also combine class-based and function-based middleware.