Skip to content

Commit b25f1b7

Browse files
committed
add middleware path restriction documentation
1 parent 9ee79db commit b25f1b7

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ const api = require('lambda-api')({ version: 'v1.0', base: 'v1' });
110110
## Recent Updates
111111
For detailed release notes see [Releases](https://github.com/jeremydaly/lambda-api/releases).
112112

113+
### v0.7: Restrict middleware execution to a certain paths
114+
Middleware now supports an optional path parameter that supports multiple paths, wildcards, and parameter matching to better control middleware execution. See [middleware](#middleware) for more information.
115+
113116
### v0.6: Support for both `callback-style` and `async-await`
114117
In additional to `res.send()`, you can now simply `return` the body from your route and middleware functions. See [Returning Responses](#returning-responses) for more information.
115118

@@ -645,7 +648,7 @@ api.options('/users/*', (req,res) => {
645648
```
646649

647650
## Middleware
648-
The API supports middleware to preprocess requests before they execute their matching routes. Middleware is defined using the `use` method and require a function with three parameters for the `REQUEST`, `RESPONSE`, and `next` callback. For example:
651+
The API supports middleware to preprocess requests before they execute their matching routes. Middleware is defined using the `use` method and requires a function with three parameters for the `REQUEST`, `RESPONSE`, and `next` callback. For example:
649652

650653
```javascript
651654
api.use((req,res,next) => {
@@ -670,7 +673,31 @@ api.use((req,res,next) => {
670673

671674
The `next()` callback tells the system to continue executing. If this is not called then the system will hang and eventually timeout unless another request ending call such as `error` is called. You can define as many middleware functions as you want. They will execute serially and synchronously in the order in which they are defined.
672675

673-
**NOTE:** Middleware can use either callbacks like `res.send()` or `return` to trigger a response to the user. Please note that calling either one of these from within a middleware function will terminate execution and return the response immediately.
676+
**NOTE:** Middleware can use either callbacks like `res.send()` or `return` to trigger a response to the user. Please note that calling either one of these from within a middleware function will return the response immediately.
677+
678+
### Restricting middleware execution to certain path(s)
679+
680+
By default, middleware will execute on every path. If you only need it to execute for specific paths, pass the path (or array of paths) as the first parameter to the `use` function.
681+
682+
```javascript
683+
// Single path
684+
api.use('/users', (req,res,next) => { next() })
685+
686+
// Wildcard path
687+
api.use('/users/*', (req,res,next) => { next() })
688+
689+
// Multiple path
690+
api.use(['/users','/posts'], (req,res,next) => { next() })
691+
692+
// Parameterized paths
693+
api.use('/users/:userId',(req,res,next) => { next() })
694+
695+
// Multiple paths with parameters and wildcards
696+
api.use(['/comments','/users/:userId','/posts/*'],(req,res,next) => { next() })
697+
```
698+
699+
Path matching checks both the supplied `path` and the defined `route`. This means that parameterize paths can be matched by either the parameter (e.g. `/users/:param1`) or by an exact matching path (e.g. `/users/123`).
700+
674701

675702
## Clean Up
676703
The API has a built-in clean up method called 'finally()' that will execute after all middleware and routes have been completed, but before execution is complete. This can be used to close database connections or to perform other clean up functions. A clean up function can be defined using the `finally` method and requires a function with two parameters for the REQUEST and the RESPONSE as its only argument. For example:

0 commit comments

Comments
 (0)