Hooks
Hooks are registered with the app.addHook method seguing the hook name and the function to be executed.
onRequest
The onRequest hook is executed before the request is processed. After validation.
app.addHook.onRequest((req) {
print("onRequest: ${req.method} ${req.path}");
});
onResponse
The onResponse hook is executed when the response is sent to the client.
app.addHook.onResponse((req, res) {
print("onResponse: response sent for ${req.method} ${req.path}");
});
preHandler
The preHandler hook is executed before the main handler route, but after validation.
app.addHook.preHandler((req, res) async {
print("preHandler: processing request before handler");
});
onError
The onError is useful if you need to some custom error logging.
app.addHook.onError((error, req, res) {
print("onError: error occurred ${error.toString()} on ${req.path}");
});
onNotFound
The onNotFound hook is executed when a route is not found.
app.addHook.onNotFound((Request req, Response res) {
res.redirect('/404');
});
app.get('/404', (Request req, Response res) {
res.status(NOT_FOUND).json({'404': 'Route not found (Auto Redirect)'});
});
Last updated on