如何使用CORS设置GCP App Engine实例? [英] How do I set up a GCP App Engine instance with CORS?

查看:53
本文介绍了如何使用CORS设置GCP App Engine实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了两个不同的GCP App Engine应用程序.一个是带有以下app.yaml的快递服务器(我们称其为foo):

I've set up two different GCP App Engine apps. One is an express server (let's call it foo) with the following app.yaml:

runtime: nodejs10
handlers:
- url: /tasks
  static_dir: /tasks
  http_headers:
    Access-Control-Allow-Origin: https://bar.appspot.com/
  secure: always

我正在从我的酒吧应用中尝试进行提取呼叫:

From my bar app, I'm trying to do a fetch call:

const response = await fetch('https://foo.appspot.com/tasks');

但是,每次尝试此操作时,Chrome都会通过已被CORS策略阻止:我在请求的资源上不显示" Access-Control-Allow-Origin"标头来阻止我的请求.错误.我也尝试过将http_header中的URL替换为'*'无济于事.为什么是这样?我想念什么?

Every time I try this, however, Chrome blocks my request with the 'has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.' error. I've also tried just replacing the URL in the http_header with '*' to no avail. Why is this? What am I missing?

经过进一步的挖掘,我发现从foo返回的标头甚至根本不包含Access-Control-Allow-Origin标头.

After some more digging, I'm finding that the headers being returned from foo don't even include the Access-Control-Allow-Origin header at all.

我终于通过仅使用npm cors软件包绕过了这个问题: https://www.npmjs.com/package/cors

Edit 2: I finally bypassed this issue by just using the npm cors package: https://www.npmjs.com/package/cors

推荐答案

截至2020年5月,我遇到了同样的问题,css文件被浏览器上的CORS策略阻止,由Google云存储提供服务.

As of May 2020, I had the same issue, css files being blocked by CORS policy on browser, served by Google cloud storage.

并且在此Gcloud Wiki页面中已解决: https://cloud.google.com/storage/docs/configuring-cors#configure-cors-bucket

And as addressed in this Gcloud wiki page: https://cloud.google.com/storage/docs/configuring-cors#configure-cors-bucket

通过在G存储桶上设置CORS,它对我有用.

It worked for me, by setting the CORS on the G storage bucket.

并且不需要app.yaml中的 handlers 东西配置,我也不是YAML btw的忠实粉丝...

And don't need the handlers stuff config in app.yaml, and I'm not a big fan of YAML btw...

这篇关于如何使用CORS设置GCP App Engine实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆