[英]How to add CORS header in Lambda/Node JS for API Gateway AWS
I am trying to enable CORS header on my Lambda/Node JS function.我正在尝试在我的 Lambda/Node JS 函数上启用 CORS 标头。 I have tried to enable CORS in the API gateway but that us not working.我曾尝试在 API 网关中启用 CORS,但我们无法正常工作。 Any idea's, My nodeJS/Lambda function code is as follows:任何想法,我的 nodeJS/Lambda 函数代码如下:
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)
};
};
Its explained in the AWS docs:它在 AWS 文档中解释:
Enabling CORS Support for Lambda or HTTP Proxy Integrations 为 Lambda 或 HTTP 代理集成启用 CORS 支持
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.