HapiJs + Google Cloud Storage 不发送请求的资源上不存在 'Access-Control-Allow-Origin' header

HapiJs + Google Cloud Storage not sending No 'Access-Control-Allow-Origin' header is present on the requested resource

我已经部署了一个 HapiJs api 到 Google App Engine,它有两个简单的 GET 端点可以从中检索 .pdf 文件,还有一个 POST将 .pdf 存储在存储桶中的端点。 我对 GCS 比较陌生,所以我确定我在这里做错了。

当我调用任一 GET 端点时,我不断收到以下错误:

Access to XMLHttpRequest at 'https://my-production-url' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

回复header,惊喜惊喜...没有'Access-Control-Allow-Origin':* header.

现在,这是我的配置:

// index.ts

bucket.setCorsConfiguration([{
    "origin": ["*"],
    "method": ["GET", "POST"],
    "responseHeader": ["Access-Control-Allow-Origin"],
    "maxAgeSeconds": 360,
  }])

...

const server: hapi.Server = new hapi.Server({
    port: 8080,
    host: 'localhost',
    routes: {
        cors: true, // also tried { cors: { origin: "ignore" } } and { cors: { origin: ["*"] } }
    }
});

...

server.route({
    method: 'GET',
    path: '/all-project-waivers/{id}',
    handler: async (request: hapi.Request, h: hapi.ResponseToolkit) => {
        try {
            // amazing stuff here
            return h.response(files).header('Access-Control-Allow-Origin', '*').code(200);
        ... more amazing stuff below

我尝试了各种方法,但似乎无法将 header 发送给客户。

据我所知,api负责将header附加到响应中,有没有人遇到同样的问题?

提前致谢

经过数小时的反复试验,我设法通过简单地将主机设置为

来修复它
0.0.0.0

而不是...

localhost

希望这能让某人在未来避免数小时的自我伤害