2

I am trying to enable CORS header on my Lambda/Node JS function. I have tried to enable CORS in the API gateway but that us not working. Any idea's, My nodeJS/Lambda function code is as follows:

exports.handler = async (event) => {
    if (event.httpMethod === 'GET') {
        return getData(event);
    }
};

const getData = event => {

    let data = {
        "home": [{
                "title": "John Doe title",
                "body": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.", 
                "image": "image/example.jpg"
            }
        ],
        "about": [{
                "title": "John is the main part 1",
                "body": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.", 
                "image": "image/example.jpg"
            }
        ]
    };

    return {
        statusCode: 200,
        body: JSON.stringify(data, null, 2)
    };
};

1 Answer 1

3

Its explained in the AWS docs:

Enabling CORS Support for Lambda or HTTP Proxy Integrations

An example is the following:

return {
    'statusCode': 200,
    'headers': {
        "Access-Control-Allow-Origin": "http://localhost:8080",
        "Access-Control-Allow-Headers": "Content-Type",
        "Access-Control-Allow-Methods": "OPTIONS,POST,GET"
    },
    'body': json.dumps(response)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.