@vendia/serverless-express
Advanced tools
Comparing version 4.3.9 to 4.3.10
{ | ||
"name": "@vendia/serverless-express", | ||
"version": "4.3.9", | ||
"version": "4.3.10", | ||
"description": "This library enables you to utilize AWS Lambda and Amazon API Gateway to respond to web and API requests using your existing Node.js application framework.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -40,2 +40,36 @@ # Serverless Express by [Vendia](https://vendia.net/) | ||
## 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 | ||
@@ -212,2 +246,2 @@ | ||
Best, | ||
The AWS Serverless team, Brett & the Vendia team | ||
The AWS Serverless team, Brett & the Vendia team |
@@ -6,4 +6,8 @@ const url = require('url') | ||
query = event.multiValueQueryStringParameters, | ||
// NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path` | ||
path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path, | ||
// NOTE: Always use event.path, if the API gateway has custom route setup, for example if my controllers path is | ||
// something like employee/services/service1, employee/services/service2 etc, if I dont have any custom path/resources setup | ||
// and directly have root/{proxy+} it works as expected, if i have a resource like /employee and child to that if there | ||
// is a resource like {proxy+} it is not working as expected and errors out with 404. This change is required to address that | ||
// specific issue. | ||
path = event.path, | ||
// NOTE: Strip base path for custom domains | ||
@@ -10,0 +14,0 @@ stripBasePath = '', |
57111
1044
246