简体   繁体   English

如何在 Lambda/Node JS 中为 API Gateway AWS 添加 CORS 标头

[英]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.

相关问题 AWS API GATEWAY &lt;=&gt; AWS Lambda CORS 问题 - AWS API GATEWAY <=> AWS Lambda CORS problem AWS Lambda API网关定义路径参数节点Js-不起作用 - AWS Lambda API Gateway Define Path Parameters Node Js - Not Working AWS Lambda 与 API 网关和 Node.js 返回 HTTP 50 - AWS Lambda with API gateway and Node.js returning HTTP 502 如何使用 API 网关集成访问 Node js AWS Lambda 中的 POST 参数? - How do I access a POST parameter in Node js AWS Lambda with API Gateway integration? 如何将 Hapi.js API 部署到 AWS Lambda 和 API 网关? - how to deploy a Hapi.js API to AWS Lambda and API gateway? 在Lambda中解析AWS API网关标头 - Parse AWS API Gateway header in Lambda 如何解析AWS Lambda(node.js)中通过AWS Api网关上传的数据的内容类型? - How to parse the Content-type of the data being uploaded through aws Api gateway in aws Lambda (node.js)? 如何从AWS Lambda Node.js 8.10异步函数向AWS API网关返回net.socket数据? - How to return net.socket data from AWS Lambda Node.js 8.10 async function to AWS API gateway? AWS API 网关与 Lambda HTTP GET 请求(Node.js)502 错误网关 - AWS API Gateway with Lambda HTTP GET Request (Node.js) 502 Bad Gateway CORS 与 AWS Lambda、DynamoDB、API 网关和 React 混淆 - CORS issue confusion with AWS Lambda, DynamoDB, API Gateway and React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM