npm install aws-serverless-config --save
Four useful middleware are provided in aws-serverless-express.
Event information received from the API Gateway in Request.headers.x-apigateway-event is stored in an encoded state. This middleware decodes this into a JSON Object.
in app.js
const apigatewayEventParser = require("aws-serverless-express").apigatewayEventParser;
app.use(apigatewayEventParser);
in app.ts
import {apigatewayEventParser} from "aws-serverless-express";
app.use(apigatewayEventParser);
using apigateway-event-parser
const event = req.headers["x-apigateway-event"];
console.info(JSON.stringify(event, null, 2));
Make aws-config in aws-serverless-express.
Merge Lambda environment varaiables and API Gateway stagevariables to Request["aws-config"]
.
If you want to local test, use dotenv.
If there is same variable between Lambda environment varaiables and API Gateway stagevariables, keep API Gateway's and drop Lambda's.
Using this does not include the value decoded in Request.headers.x-apigateway-event.
If you need to do this, you can declare it as app.use(apigatewayEventParser, awsConfig)
.
in app.js
const awsConfig = require("aws-serverless-express").awsConfig;
app.use(awsConfig);
in app.ts
import {awsConfig} from "aws-serverless-express";
app.use(awsConfig);
using aws-config
const config = req["aws-config"]
console.info(JSON.stringify(config, null, 2));
This is a middleware that provides a function to output the log so that CloudWatch can check it if it is declared as one of EVENT, REQUEST and SIMPLE in PRINT_LOG in Lambda Environment Variables or API Gateway StageVariables.
- EVENT : Print apigateway event.
- REQUEST : Print express request.
- SIMPLE : Print body, headers, query, url, params and aws-config from express request.
To do this you need to run aws-config first.
app.use(awsConfig, printLog);
in app.js
const awsConfig = require("aws-serverless-express").awsConfig;
const printLog = require("aws-serverless-express").printLog;
app.use(awsConfig, printLog);
in app.ts
import {awsConfig, pringLog} from "aws-serverless-express";
app.use(awsConfig, pringLog);
This will cause CloudWatch to display the resulting Error.
If you do not do anything, CloudWatch will show an error. If you use another ErrorHandler, you may not want to print it. In this case, it is possible to print using this.
app.use(awsConfig, printLog);
in app.js
const printError = require("aws-serverless-express").printError;
function yourErrorHandler(err, req, res, next) { ... };
app.use(printError, yourErrorHandler);
in app.ts
import {Request , Response , NextFunction} from 'express';
import {printError} from "aws-serverless-express";
function yourErrorHandler(err: any, req: Request, res: Response, next: NextFunction) { ... };
app.use(awsConfig, yourErrorHandler);
Toggler is available under the MIT license. See the LICENSE file for more info.