Skip to content

Instantly share code, notes, and snippets.

@redgeoff
Last active August 5, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redgeoff/e89cef539761ce08a91c4ceb833ae546 to your computer and use it in GitHub Desktop.
Save redgeoff/e89cef539761ce08a91c4ceb833ae546 to your computer and use it in GitHub Desktop.
lamdbaToExpress - lamdbaToExpress.js
// Convert from a Lambda to an ExpressJS handler
const lambdaToExpress = async (handler) => {
return async (req, res) => {
// Repackage the express parameters as an event object for the Lambda
const event = {
body: JSON.stringify(req.body),
headers: req.headers,
queryStringParameters: req.query,
pathParameters: req.params
};
const handlerRes = await handler(event);
// Instruct express to use the response from the Lambda
res.status(handlerRes.statusCode).json(JSON.parse(handlerRes.body))
};
}
export default lambdaToExpress;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment