Middleware
DinoSsr is an experimental work in progress and subject to change!
Middleware can be added to DinoSsr by attaching
VelociRouter
handles to the internal router
after initiating
the server.
Before
router.use
will attach the handle before any
auto-generated routes.
dinossr.router.use(({request, response}) => {
const pattern = new URLPattern('/api/:id/', request.url);
const match = pattern.exec(request.url);
if (match && request.method === 'GET') {
return Response.json({id: match.pathname.groups.id});
}
});
All requests will pass through a use
handle.
After
Other methods like router.get
require a pattern. These handles
are attached after all auto-generated routes.
dinossr.router.get('/api/*', ({response}) => {
try {
response.headers.set('x-api-version', '1.0');
} catch { /* No response or immutable headers */ }
});
See the VelociRouter documentation for usage.
Middleware is ideal for:
- Setting up the context data objects
- Handling API endpoints and
POST
requests - Anything that requires large 3rd party libraries †
† middleware is never bundled.