aws-lambda-cors

CORS Library for AWS Lambda with APIGateway.

Usage no npm install needed!

<script type="module">
  import awsLambdaCors from 'https://cdn.skypack.dev/aws-lambda-cors';
</script>

README

AWS Lambda CORS

Build Downloads Version License
dependencies Status devDependencies Status

CORS Library for AWS Lambda with APIGateway.

Installation

npm install aws-lambda-cors --save

Usage

import { cors } from 'aws-lambda-cors'

const originLambdaHandler = async (event, ctx) => {
  return {
    statusCode: 200,
    body: '',
  })
}

// Also OK.
//
// const originLambdaHandler = (event, ctx, callback) => {
//   callback(null, {
//     statusCode: 200,
//     body: '',
//   })
// }

export const lambdaHandler = cors({
  allowCredentials: true,
  allowOrigins: [
    'http://localhost:8000',
    'https://wani.kr',
  ],
  allowMethods: [
    'OPTIONS',
    'HEAD',
    'GET',
    'POST',
  ],
  allowHeaders: [
    'Authorization',
    'Content-Type',
  ],
  allowCredentials: true,
})(originLambdaHandler)