Skip to content

Instantly share code, notes, and snippets.

@alexdebrie
Created January 11, 2018 16:53
Show Gist options
  • Save alexdebrie/952fb62b7a5d0f735904fd4da94b7062 to your computer and use it in GitHub Desktop.
Save alexdebrie/952fb62b7a5d0f735904fd4da94b7062 to your computer and use it in GitHub Desktop.
Middy CORS example
# 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