Last active
August 5, 2021 16:00
-
-
Save redgeoff/e89cef539761ce08a91c4ceb833ae546 to your computer and use it in GitHub Desktop.
lamdbaToExpress - lamdbaToExpress.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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