Lifecycle
Mapl request lifecycle can be illustrated as follows:
Middlewares
Middlewares are functions that get executed on every request, after the route matching phase.
The above snippet adds the Server
header to every response if the request path matched any of the defined patterns.
Note that middlewares share the same limitation as handlers, as if a middleware function returns a
Promise object it must be marked as an async
function.
Setters
Setter is a type of middleware that its returned value will be attached to the Context object.
The value can be used in any handler or middleware after it:
Exceptions
Exception is a much faster and lighter version of Error.
To handle exceptions, use app.catch
or app.catchAll
:
Exceptions are meant to be used with validators and parsers to handle edge cases.
Validators
Validator is a type of middleware to validate requests.
If an exception is returned, the correct exception handler will be called.
If no exception handler is registered for that error (including the handler for all exceptions),
a 400 Bad Request
response will be returned.
Parsers
Parsers can return exceptions like validators, but if the return value is anything other than an exception it gets attached to the context.