Reputation: 3340
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)
};
};
Upvotes: 2
Views: 3190
Reputation: 238199
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)
}
Upvotes: 3