Created
January 11, 2018 16:53
-
-
Save alexdebrie/952fb62b7a5d0f735904fd4da94b7062 to your computer and use it in GitHub Desktop.
Middy CORS example
This file contains hidden or 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
# handler.js | |
const middy = require('middy') | |
const { cors } = require('middy/middlewares') | |
// This is your common handler, no way different than what you are used to do every day | |
// in AWS Lambda | |
const processPayment = (event, context, callback) => { | |
// we don't need to deserialize the body ourself as a middleware will be used to do that | |
const { creditCardNumber, expiryMonth, expiryYear, cvc, nameOnCard, amount } = event.body | |
// do stuff with this data | |
// ... | |
return callback(null, { result: 'success', message: 'payment processed correctly'}) | |
} | |
// Let's "middyfy" our handler, then we will be able to attach middlewares to it | |
const handler = middy(processPayment) | |
.use(cors()) // Adds CORS headers to responses | |
module.exports = { handler } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment