4. Middleware¶
Middleware runs before and after every request, in registration order. Baldr ships a few useful middleware and a simple interface for adding your own.
Built-in middleware¶
| src/main.cpp | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
The two middleware above:
RequestIdMiddleware— attaches anX-Request-Idheader to every response. If the request already has one, it is reused; otherwise a fresh id is generated.LoggingMiddleware— emits a one-line access log entry per request.
Custom middleware¶
Implement IMiddleware::Handle:
| src/middleware.hpp | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Register it the same way as the built-ins:
src/main.cpp
app->Use<TimingMiddleware>();
Reading route metadata¶
Inside a middleware, req.route.options carries the metadata attached via RouteRegistration::WithMetadata — see Route options for the full list.
Next¶
Continue with 5. Static files.