被 CORS 阻止,也无法使用 Amplify Functions 访问 REST API 端点
blocked by CORS and also fail accessing REST API endpoint with Amplify Functions
我正在按照此 link 部署具有 Amplify 函数的 Apollo GraphQL 服务器:
https://dev.to/aws/10-minute-tutorial-deploy-an-apollo-graphql-server-with-amplify-functions-38p1
然而,当我运行“npm start”时,它无法访问'http://localhost:3000',显示:
Access to fetch at 'https://******.execute-api.us-east-1.amazonaws.com/dev/graphql'
from origin 'http://localhost:3000' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested resource. If an
opaque response serves your needs, set the request's mode to 'no-cors' to fetch
the resource with CORS disabled.
Failed to load resource: net::ERR_FAILED
******.execute-api.us-east-1.amazonaws.com/dev/graphql:1
以下是我在 index.js 中为 Lambda 设置导出处理程序的方法:
exports.handler = server.createHandler({
cors: {
origin: "*",
credentials: true, // I tried setting false is also the same
},
});
我不确定 CORS 是否是根本原因,因为我认为上述配置应该可以正常工作。我怀疑 API 端点出了问题。我看到使用浏览器访问 REST API 端点失败,这是 aws-exports.js:
中的 link
awsmobile.aws_cloud_logic_custom[0] = "******.execute-api.us-east-1.amazonaws.com/dev"
显示:
{"message":"Missing Authentication Token"}
而且我也无法访问“******.execute-api.us-east-1.amazonaws.com/dev/graphql”,显示:
{"message": "Internal server error"}
是不是因为缺少AWS签名?我还需要为身份验证配置什么吗?
折腾了几天,找到了根本原因。这既不是 CORS 也不是身份验证问题。
根据 https://docs.aws.amazon.com/apigateway/latest/developerguide/amazon-api-gateway-using-stage-variables.html,预计会看到“缺少身份验证令牌”,因为我将访问 child 资源而不是端点本身。这里的child资源是“******.execute-api.us-east-1.amazonaws.com/dev/graphql”.
The Invoke URL link points to the root resource of the API in its beta
stage. Navigating to the URL by choosing the link calls the beta stage
GET method on the root resource. If methods are defined on child
resources and not on the root resource itself, choosing the Invoke URL
link returns a {"message":"Missing Authentication Token"} error
response. In this case, you must append the name of a specific child
resource to the Invoke URL link.
为什么访问"******.execute-api.us-east-1.amazonaws.com/dev/graphql",我查了一下发现是这个错误云观察日志。这是一个很好的调试工具。
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'apollo-server-lambda'",
"amplify push" 无法识别这个包,因为我使用的是节点 v6.9.5 的终端。在我将它更新到最新节点并“放大推送”后,问题就解决了。
CORS 错误令人分心,在解决上述问题后它不会出现。 CORS header 不会被添加,因为 index.js 编译失败。
我正在按照此 link 部署具有 Amplify 函数的 Apollo GraphQL 服务器: https://dev.to/aws/10-minute-tutorial-deploy-an-apollo-graphql-server-with-amplify-functions-38p1
然而,当我运行“npm start”时,它无法访问'http://localhost:3000',显示:
Access to fetch at 'https://******.execute-api.us-east-1.amazonaws.com/dev/graphql'
from origin 'http://localhost:3000' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested resource. If an
opaque response serves your needs, set the request's mode to 'no-cors' to fetch
the resource with CORS disabled.
Failed to load resource: net::ERR_FAILED
******.execute-api.us-east-1.amazonaws.com/dev/graphql:1
以下是我在 index.js 中为 Lambda 设置导出处理程序的方法:
exports.handler = server.createHandler({
cors: {
origin: "*",
credentials: true, // I tried setting false is also the same
},
});
我不确定 CORS 是否是根本原因,因为我认为上述配置应该可以正常工作。我怀疑 API 端点出了问题。我看到使用浏览器访问 REST API 端点失败,这是 aws-exports.js:
中的 linkawsmobile.aws_cloud_logic_custom[0] = "******.execute-api.us-east-1.amazonaws.com/dev"
显示:
{"message":"Missing Authentication Token"}
而且我也无法访问“******.execute-api.us-east-1.amazonaws.com/dev/graphql”,显示:
{"message": "Internal server error"}
是不是因为缺少AWS签名?我还需要为身份验证配置什么吗?
折腾了几天,找到了根本原因。这既不是 CORS 也不是身份验证问题。
根据 https://docs.aws.amazon.com/apigateway/latest/developerguide/amazon-api-gateway-using-stage-variables.html,预计会看到“缺少身份验证令牌”,因为我将访问 child 资源而不是端点本身。这里的child资源是“******.execute-api.us-east-1.amazonaws.com/dev/graphql”.
The Invoke URL link points to the root resource of the API in its beta stage. Navigating to the URL by choosing the link calls the beta stage GET method on the root resource. If methods are defined on child resources and not on the root resource itself, choosing the Invoke URL link returns a {"message":"Missing Authentication Token"} error response. In this case, you must append the name of a specific child resource to the Invoke URL link.
为什么访问"******.execute-api.us-east-1.amazonaws.com/dev/graphql",我查了一下发现是这个错误云观察日志。这是一个很好的调试工具。
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'apollo-server-lambda'",
"amplify push" 无法识别这个包,因为我使用的是节点 v6.9.5 的终端。在我将它更新到最新节点并“放大推送”后,问题就解决了。
CORS 错误令人分心,在解决上述问题后它不会出现。 CORS header 不会被添加,因为 index.js 编译失败。