Nested Routes
How to work with nested routes in NEMO middleware
Nested Routes
NEMO supports hierarchical organization of middleware through nested routes, allowing you to create advanced routing patterns while maintaining clean code structure.
Basic Nested Routes
You can define nested routes by using objects that contain both a middleware
property and additional route keys:
In this example:
- The parent middleware runs for
/admin
and any matching child routes - The child middleware runs only for
/admin/users
- Both middlewares execute in sequence for matching routes
Execution Order
When a request matches a nested route, NEMO executes the middleware in this order:
- Global
before
middleware (if defined) - Root path middleware (
/
) for all non-root requests - Parent middleware
- Child middleware
- Global
after
middleware (if defined)
If any middleware in the chain returns a response (like a redirect), the chain stops and that response is returned immediately.
Chain Breaking
When a parent middleware returns a response, child middleware will not be executed:
Deep Nesting with Parameters
You can create deeply nested routes with URL parameters:
In this advanced example, a path like /shop/categories/electronics/products/laptop
would execute all middleware in the chain, with event.params
containing:
categoryId: "electronics"
productId: "laptop"
Important Notes
-
Top-level routes without nesting only match exact paths by default.
-
Parent routes with nested children will match both the exact path and child paths.
-
Parameter matching follows the same rules as regular route matching - parent parameters are available to child routes.