How execution stacks work in Lambda API
mainLambda API uses execution stacks to efficiently process middleware. Stacks are automatically created when you use standard route methods, METHOD(), or use().
Key behaviors:
- Inheritance: Execution stacks inherit middleware from matching routes and methods higher up the stack, building a unique final stack for each route.
- Order Matters: Routes defined before global middleware will not include that middleware in their execution stack. The same applies to wildcard-based routes.
- Path-based Middleware: Middleware attached to parameterized paths (e.g.,
/users/:userId) creates mount points. This means a middleware defined for/users/:userIdwill not execute if the request matches a static path like/users/testunless specifically configured. - Debugging: If you use named functions for your middleware, you can inspect the
REQUEST.stackproperty, which returns an array of function names in the order they are executed.