Skip to content

Commit 35db173

Browse files
committed
docs: add async setup example
1 parent 55c87df commit 35db173

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,40 @@ const app = require('./app')
3737
exports.handler = serverlessExpress({ app })
3838
```
3939

40+
## Async setup Lambda handler
41+
42+
If your application needs to perform some common bootstrap tasks such as connecting to a database before the request is forward to the API, you can use the following pattern (also available in [this example](https://github.com/vendia/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/src/lambda-async-setup.js)):
43+
44+
```js
45+
// lambda.js
46+
require('source-map-support/register')
47+
const serverlessExpress = require('@vendia/serverless-express')
48+
const app = require('./app')
49+
50+
let serverlessExpressInstance
51+
52+
function asyncTask () {
53+
return new Promise((resolve) => {
54+
setTimeout(() => resolve('connected to database'), 1000)
55+
})
56+
}
57+
58+
async function setup (event, context) {
59+
const asyncValue = await asyncTask()
60+
console.log(asyncValue)
61+
serverlessExpressInstance = serverlessExpress({ app })
62+
return serverlessExpressInstance(event, context)
63+
}
64+
65+
function handler (event, context) {
66+
if (serverlessExpressInstance) return serverlessExpressInstance(event, context)
67+
68+
return setup(event, context)
69+
}
70+
71+
exports.handler = handler
72+
```
73+
4074
## 4.x
4175

4276
1. Improved API - Simpler for end-user to use and configure.
@@ -208,4 +242,4 @@ On 11/30, the AWS Serverless Express library moved from AWS to [Vendia](https://
208242
We believe this is the best course of action to ensure that customers using this library get the best possible support in the future. To learn more about this move or become a maintainer of the new Serverless Express library, reach out to us through a GitHub issue on this repository.
209243

210244
Best,
211-
The AWS Serverless team, Brett & the Vendia team
245+
The AWS Serverless team, Brett & the Vendia team

examples/basic-starter-api-gateway-v2/scripts/local.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const callback = (e, v) => {
1313
process.exit(0)
1414
}
1515
const server = lambdaFunction.handler(apiGatewayEvent, context, callback)
16+
server.then((v) => {
17+
console.info(v)
18+
process.exit(0)
19+
}).catch(e => {
20+
console.error(e)
21+
process.exit(0)
22+
})
1623

1724
process.stdin.resume()
1825

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require('source-map-support/register')
2+
const serverlessExpress = require('@vendia/serverless-express')
3+
const app = require('./app')
4+
5+
let serverlessExpressInstance
6+
7+
function asyncTask () {
8+
return new Promise((resolve) => {
9+
setTimeout(() => resolve('connected to database'), 1000)
10+
})
11+
}
12+
13+
async function setup (event, context) {
14+
const asyncValue = await asyncTask()
15+
console.log(asyncValue)
16+
serverlessExpressInstance = serverlessExpress({ app })
17+
return serverlessExpressInstance(event, context)
18+
}
19+
20+
function handler (event, context) {
21+
if (serverlessExpressInstance) return serverlessExpressInstance(event, context)
22+
23+
return setup(event, context)
24+
}
25+
26+
exports.handler = handler

0 commit comments

Comments
 (0)