Skip to content

Commit

Permalink
docs: add async setup example
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Aug 28, 2021
1 parent 55c87df commit 35db173
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ const app = require('./app')
exports.handler = serverlessExpress({ app })
```

## Async setup Lambda handler

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)):

```js
// lambda.js
require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

let serverlessExpressInstance

function asyncTask () {
return new Promise((resolve) => {
setTimeout(() => resolve('connected to database'), 1000)
})
}

async function setup (event, context) {
const asyncValue = await asyncTask()
console.log(asyncValue)
serverlessExpressInstance = serverlessExpress({ app })
return serverlessExpressInstance(event, context)
}

function handler (event, context) {
if (serverlessExpressInstance) return serverlessExpressInstance(event, context)

return setup(event, context)
}

exports.handler = handler
```

## 4.x

1. Improved API - Simpler for end-user to use and configure.
Expand Down Expand Up @@ -208,4 +242,4 @@ On 11/30, the AWS Serverless Express library moved from AWS to [Vendia](https://
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.

Best,
The AWS Serverless team, Brett & the Vendia team
The AWS Serverless team, Brett & the Vendia team
7 changes: 7 additions & 0 deletions examples/basic-starter-api-gateway-v2/scripts/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const callback = (e, v) => {
process.exit(0)
}
const server = lambdaFunction.handler(apiGatewayEvent, context, callback)
server.then((v) => {
console.info(v)
process.exit(0)
}).catch(e => {
console.error(e)
process.exit(0)
})

process.stdin.resume()

Expand Down
26 changes: 26 additions & 0 deletions examples/basic-starter-api-gateway-v2/src/lambda-async-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

let serverlessExpressInstance

function asyncTask () {
return new Promise((resolve) => {
setTimeout(() => resolve('connected to database'), 1000)
})
}

async function setup (event, context) {
const asyncValue = await asyncTask()
console.log(asyncValue)
serverlessExpressInstance = serverlessExpress({ app })
return serverlessExpressInstance(event, context)
}

function handler (event, context) {
if (serverlessExpressInstance) return serverlessExpressInstance(event, context)

return setup(event, context)
}

exports.handler = handler

0 comments on commit 35db173

Please sign in to comment.