amazon-web-services - 无服务器 aws httpApi cors 设置
问题描述
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: '20201221'
role: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
httpApi:
cors:
allowedOrigins:
- '*'
allowedMethods:
- GET
- OPTIONS
- POST
- PUT
- DELETE
allowedHeaders:
- Content-Type
- X-Amz-Date
- Authorization
- X-Api-Key
- X-Amz-Security-Token
- X-Amz-User-Agent
- X-Transaction-Key
- Access-Control-Allow-Origin
- Access-Control-Allow-Methods
- Access-Control-Allow-Headers
- Access-Control-Allow-Credentials
functions:
getOTP:
handler: xxxx/xxxx.yyyy
events:
- httpApi:
path: /xxxx/yyyy
method: POST
- httpApi:
path: /xxxx/yyyy
method: OPTIONS
module.exports.yyyy= async (event) => {
const body = JSON.parse(event.body);
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
"Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
"Access-Control-Allow-Headers" : "*",
"Access-Control-Allow-Methods": "*",
"Content-Type": "application/json"
},
body: JSON.stringify({
message: "yayay",
}),
};
抱歉英语不好。我正在使用无服务器框架来构建 API 服务。我只是按照无服务器文档在 aws 上构建 httpApi 事件。但是 cors 设置不起作用。这是我的 yml 文件和示例代码。
解决方案
您是否尝试在功能级别而不是服务级别设置 cors 配置?
functions:
getOTP:
handler: xxxx/xxxx.yyyy
events:
- http:
path: /xxxx/yyyy
method: POST
cors: true
推荐阅读
- c++ - 将 push_back 用于向量
> 在 C++ 中正确使用 for & while 循环? - java - 检查包含 avro 事件的字节数组是否对应于特定模式
- java - 从库 jar 中获取执行位置
- javascript - 如何将一些代码传递给接受回调的函数?
- javascript - 如何在 React 中为项目添加过渡效果
- javascript - 将现有应用程序转换为 react 会产生意外的令牌“<”
- node.js - Node.js 和 Redis:如何从回调中将项目添加到函数范围的对象?
- attributes - 在 java 中更改 KeyCloak 用户属性的值
- aem - $ mvn clean install -PautoInstallSinglePackage
- ms-word - word 对象模型 - 单击 FILE 时的活动文档事件