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
Copy file name to clipboardExpand all lines: README.md
+91-14
Original file line number
Diff line number
Diff line change
@@ -9,24 +9,24 @@
9
9
10
10
Lambda API is a lightweight web framework for use with AWS API Gateway and AWS Lambda using Lambda Proxy integration. This closely mirrors (and is based on) other routers like Express.js, but is significantly stripped down to maximize performance with Lambda's stateless, single run executions.
11
11
12
+
**IMPORTANT:** There is a [breaking change](#breaking-change-in-v03) in v0.3 that affects instantiation.
@@ -38,6 +38,21 @@ Lambda API has **ONE** dependency. We use [Bluebird](http://bluebirdjs.com/docs/
38
38
39
39
Lambda API was written to be extremely lightweight and built specifically for serverless applications using AWS Lambda. It provides support for API routing, serving up HTML pages, issuing redirects, and much more. It has a powerful middleware and error handling system, allowing you to implement everything from custom authentication to complex logging systems. Best of all, it was designed to work with Lambda's Proxy Integration, automatically handling all the interaction with API Gateway for you. It parses **REQUESTS** and formats **RESPONSES** for you, allowing you to focus on your application's core functionality, instead of fiddling with inputs and outputs.
40
40
41
+
## Breaking Change in v0.3
42
+
Please note that the invocation method has been changed. You no longer need to use the `new` keyword to instantiate Lambda API. It can now be instantiated in one line:
43
+
44
+
```javascript
45
+
constapi=require('lambda-api')()
46
+
```
47
+
48
+
`lambda-api` returns a `function` now instead of a `class`, so options can be passed in as its only argument:
**IMPORTANT:** Upgrading to v0.3.0 requires either removing the `new` keyword or switching to the one-line format. This provides more flexibility for instantiating Lambda API in future releases.
55
+
41
56
## Lambda Proxy integration
42
57
Lambda Proxy Integration is an option in API Gateway that allows the details of an API request to be passed as the `event` parameter of a Lambda function. A typical API Gateway request event with Lambda Proxy Integration enabled looks like this:
43
58
@@ -104,13 +119,11 @@ The API automatically parses this information to create a normalized `REQUEST` o
104
119
105
120
## Configuration
106
121
107
-
Include the `lambda-api` module into your Lambda handler script and initialize an instance. You can initialize the API with an optional `version` which can be accessed via the `REQUEST` object and a `base` path. The base path can be used to route multiple versions to different instances.
122
+
Require the `lambda-api` module into your Lambda handler script and instantiate it. You can initialize the API with an optional `version` which can be accessed via the `REQUEST` object and a `base` path.
108
123
109
124
```javascript
110
-
constAPI=require('lambda-api') // API library
111
-
112
-
// Init API instance with optional version and base path
113
-
constapi=newAPI({ version:'v1.0', base:'v1' });
125
+
// Require the framework and instantiate it with optional version and base parameters
@@ -156,6 +169,7 @@ The `REQUEST` object contains a parsed and normalized request from API Gateway.
156
169
-`route`: The matched route of the request
157
170
-`requestContext`: The `requestContext` passed from the API Gateway
158
171
-`namespace` or `ns`: A reference to modules added to the app's namespace (see [namespaces](#namespaces))
172
+
-`cookies`: An object containing cookies sent from the browser (see the [cookie](#cookie)`RESPONSE` method)
159
173
160
174
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 `send` methods triggers the API to return data to the API Gateway. The `send` method accepts one parameter and sends the contents through as is, e.g. as an object, string, integer, etc. AWS Gateway expects a string, so the data should be converted accordingly.
186
200
187
201
### json
188
-
There is a `json` convenience method for the `send` method that will set the headers to `application\json` as well as perform `JSON.stringify()` on the contents passed to it.
202
+
There is a `json` convenience method for the `send` method that will set the headers to `application/json` as well as perform `JSON.stringify()` on the contents passed to it.
189
203
190
204
```javascript
191
205
api.get('/users', function(req,res) {
192
206
res.json({ message:'This will be converted automatically' })
193
207
})
194
208
```
195
209
210
+
### jsonp
211
+
There is a `jsonp` convenience method for the `send` method that will set the headers to `application/json`, perform `JSON.stringify()` on the contents passed to it, and wrap the results in a callback function. By default, the callback function is named `callback`.
212
+
213
+
```javascript
214
+
res.jsonp({ foo:'bar' })
215
+
// => callback({ "foo": "bar" })
216
+
217
+
res.status(500).jsonp({ error:'some error'})
218
+
// => callback({ "error": "some error" })
219
+
```
220
+
221
+
The default can be changed by passing in `callback` as a URL parameter, e.g. `?callback=foo`.
222
+
223
+
```javascript
224
+
// ?callback=foo
225
+
res.jsonp({ foo:'bar' })
226
+
// => foo({ "foo": "bar" })
227
+
```
228
+
229
+
You can change the default URL parameter using the optional `callback` option when initializing the API.
Convenience method for setting cookies. This method accepts a `name`, `value` and an optional `options` object with the following parameters:
286
+
287
+
| Property | Type | Description |
288
+
| -------- | ---- | ----------- |
289
+
| domain |`String`| Domain name to use for the cookie. This defaults to the current domain. |
290
+
| expires |`Date`| The expiration date of the cookie. Local dates will be converted to GMT. Creates session cookie if this value is not specified. |
291
+
| httpOnly |`Boolean`| Sets the cookie to be accessible only via a web server, not JavaScript. |
292
+
| maxAge |`Number`| Set the expiration time relative to the current time in milliseconds. Automatically sets the `expires` property if not explicitly provided. |
293
+
| path |`String`| Path for the cookie. Defaults to "/" for the root directory. |
294
+
| secure |`Boolean`| Sets the cookie to be used with HTTPS only. |
295
+
|sameSite |`Boolean` or `String`| Sets the SameSite value for cookie. `true` or `false` sets `Strict` or `Lax` respectively. Also allows a string value. See https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1|
296
+
297
+
The `name` attribute should be a string (auto-converted if not), but the `value` attribute can be any type of value. The `value` will be serialized (if an object, array, etc.) and then encoded using `encodeURIComponent` for safely assigning the cookie value. Cookies are automatically parsed, decoded, and available via the `REQUEST` object (see [REQUEST](#request)).
298
+
299
+
**NOTE:** The `cookie()` method only sets the header. A execution ending method like `send()`, `json()`, etc. must be called to send the response.
Convenience method for expiring cookies. Requires the `name` and optional `options` object as specified in the [cookie](#cookie) method. This method will automatically set the expiration time. However, most browsers require the same options to clear a cookie as was used to set it. E.g. if you set the `path` to "/admin" when you set the cookie, you must use this same value to clear it.
**NOTE:** The `clearCookie()` method only sets the header. A execution ending method like `send()`, `json()`, etc. must be called to send the response.
316
+
240
317
## Path Parameters
241
318
Path parameters are extracted from the path sent in by API Gateway. Although API Gateway supports path parameters, the API doesn't use these values but insteads extracts them from the actual path. This gives you more flexibility with the API Gateway configuration. Path parameters are defined in routes using a colon `:` as a prefix.
0 commit comments