serverless-express: Can't get the API Gateway event object
I use aws-serverless-express/middleware but can’t get the API Gateway event object. Here is my code.
...
import awsServerlessExpress from 'aws-serverless-express';
import awsServerlessExpressMiddleware from 'aws-serverless-express/middleware';
...
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
api.use(awsServerlessExpressMiddleware.eventContext());
app.use('/', api);
const server = awsServerlessExpress.createServer(app);
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
When i use access req.apiGateway.event, error has occurred like below.
TypeError: Cannot read property 'event' of undefined
Am i missing something? or is this bug?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15
In case any other lost souls who are encountering
Missing x-apigateway-event or x-apigateway-context header(s)
when doing local testing (e.g. from Postman) find their way here… You have to set both of thex-apigateway-event
andx-apigateway-context
headers in Postman. The important thing is their values have to beSo for instance, if the header key is:
x-apigateway-event
Then the value should be%7B%22data%22%3A%22Sand%22%2C%22public_key%22%3A%22key%22%7D
Note, there are no quotes around the encoded and stringified value!
You have to set both
x-apigateway-event
andx-apigateway-context
headers because if you peek in yournode_modules/aws-serverless-express/src/middleware.js
file you’ll see it has the following lines:aws-serverless-express
library is being deprecated and moving to vendia. Here is how it should be done using vendia libraryGuide: https://github.com/vendia/serverless-express#accessing-the-event-and-context-objects
Source: https://www.npmjs.com/package/aws-serverless-express#:~:text=AWS Serverless Express library is moving to Vendia and will be rebranded to serverless-express. Similarly%2C the aws-serverless-express NPM package will be deprecated in favor of a new serverless-express package
No. I didn’t…