[SOLVED] Access requestContext from express route with serverless-http and serverless-offline

I am modelling my API after this tutorial, using serverless-http with expressJS and serverless-offline for local development. Inside an express route, I need to be able to access the event’s requestContext for authentication information. I can see the requestContext is available and the serverless-http plugin is providing some pieces of that context to the request object, but I cannot figure out how to access the identity and authorizor properties of the object from inside an express route.

Any help is much appreciated. Thank you!

1 Like

I figured this out thanks to an answer in another forum and reading the docs more closely. In case anyone else comes across this later there is the solution:

In your main handler, you can provide a function that modifies the request object and has the event context provided.

module.exports.handler = serverless(app, {
   request: function (req, event, context) {
       // context.callbackWaitsForEmptyEventLoop = false;
       req.event = event;
       req.context = context;
   },
});
4 Likes

Thanks very much! This just solved a major problem for me.