From 3d718187f1ce1fdb77c641337aa51e62b50fb646 Mon Sep 17 00:00:00 2001 From: Andreas Zoellner Date: Sat, 23 Feb 2019 13:44:23 -0800 Subject: [PATCH] add examples for different resolutionMode options. related to #173 --- README.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 53a0aa65..569c909f 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event [Package and create your Lambda function](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html), then configure a simple proxy API using Amazon API Gateway and integrate it with your Lambda function. -## Quick Start/Example +## Quick Start Want to get up and running quickly? [Check out our basic starter example](examples/basic-starter) which includes: @@ -33,6 +33,8 @@ Want to get up and running quickly? [Check out our basic starter example](exampl - [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model)/[CloudFormation](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/) template - Helper scripts to configure, deploy, and manage your application +## Examples + ### Getting the API Gateway event object This package includes middleware to easily get the event object Lambda receives from API Gateway @@ -44,6 +46,39 @@ app.get('/', (req, res) => { }) ``` +### Using resolveMode `PROMISE` with async handler + +```js +// lambda.js +'use strict' +const awsServerlessExpress = require('aws-serverless-express') +const app = require('./app') +const server = awsServerlessExpress.createServer(app) + +// note that callback the promise is always resolved +// i.e. errors still lead to a resolved promise +// The resolved promise has a a statusCode property that can be checked for error codes + +exports.handler = async (event, context) => { return awsServerlessExpress.proxy(server, event, context, 'PROMISE') } +``` + +### Using resolveMode `CALLBACK` with callback function + +```js +// lambda.js +'use strict' +const awsServerlessExpress = require('aws-serverless-express') +const app = require('./app') +const server = awsServerlessExpress.createServer(app) + +exports.handler = (event, context, callback) => { + // note that callback is always called with (null, responseObject) + // i.e. errors still lead to a "successful" callback. + // The responseObject has a a statusCode property that can be checked for error codes + awsServerlessExpress.proxy(server, event, context, 'CALLBACK', callback) +} +``` + ### Is AWS serverless right for my app? #### Benefits