You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For detailed release notes see [Releases](https://github.com/jeremydaly/lambda-api/releases).
111
112
113
+
### v0.7: Restrict middleware execution to 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
+
112
116
### v0.6: Support for both `callback-style` and `async-await`
113
117
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.
114
118
@@ -320,6 +324,7 @@ The `REQUEST` object contains a parsed and normalized request from API Gateway.
320
324
321
325
-`app`: A reference to an instance of the app
322
326
-`version`: The version set at initialization
327
+
-`id`: The awsRequestId from the Lambda `context`
323
328
-`params`: Dynamic path parameters parsed from the path (see [path parameters](#path-parameters))
324
329
-`method`: The HTTP method of the request
325
330
-`path`: The path passed in by the request including the `base` and any `prefix` assigned to routes
@@ -336,6 +341,7 @@ The `REQUEST` object contains a parsed and normalized request from API Gateway.
336
341
-`auth`: An object containing the `type` and `value` of an authorization header. Currently supports `Bearer`, `Basic`, `OAuth`, and `Digest` schemas. For the `Basic` schema, the object is extended with additional fields for username/password. For the `OAuth` schema, the object is extended with key/value pairs of the supplied OAuth 1.0 values.
337
342
-`namespace` or `ns`: A reference to modules added to the app's namespace (see [namespaces](#namespaces))
338
343
-`cookies`: An object containing cookies sent from the browser (see the [cookie](#cookiename-value-options)`RESPONSE` method)
344
+
-`context`: Reference to the `context` passed into the Lambda handler function
339
345
340
346
The request object can be used to pass additional information through the processing chain. For example, if you are using a piece of authentication middleware, you can add additional keys to the `REQUEST` object with information about the user. See [middleware](#middleware) for more information.
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:
646
652
647
653
```javascript
648
654
api.use((req,res,next) => {
@@ -667,7 +673,31 @@ api.use((req,res,next) => {
667
673
668
674
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.
669
675
670
-
**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.
Path matching checks both the supplied `path` and the defined `route`. This means that parameterized paths can be matched by either the parameter (e.g. `/users/:param1`) or by an exact matching path (e.g. `/users/123`).
700
+
671
701
672
702
## Clean Up
673
703
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:
@@ -814,4 +844,4 @@ Routes must be configured in API Gateway in order to support routing to the Lamb
814
844
Simply create a `{proxy+}` route that uses the `ANY` method and all requests will be routed to your Lambda function and processed by the `lambda-api` module. In order for a "root" path mapping to work, you also need to create an `ANY` route for `/`.
815
845
816
846
## Contributions
817
-
Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/lambda-api/issues) for suggestions and bug reports.
847
+
Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/lambda-api/issues) for suggestions and bug reports or create a pull request.
0 commit comments