Google Cloud节点应用无法连接跨源请求被阻止

时间:2019-11-19 15:03:42

标签: node.js google-cloud-platform vuejs2 cors

我刚刚将node.js-vue.2.0应用程序部署到了Google Cloud。

vue.js 2.0应用程序位于公共节点目录中。一切运行正常,除了我对Web服务的所有查询均失败,在惯用的app.js节点文件中,您知道..没有对其他节点服务器的调用,这就是为什么我什至尝试了localhost:8080作为服务器名称。

Glove algoritm

通过该应用程序完成的所有后端查询均失败:

POST
http://localhost:8080/getUsers

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/getUsers. (Reason: CORS request did not succeed).

我还管理了该应用程序以调用google cloud服务器url:相同的错误。

POST http://cedar-network-259109.appspot.com:8080/getUsers

它也不起作用。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/getUsers. (Reason: CORS request did not succeed).

问题: 我是否必须使用奇怪的命令在Google云应用上为此文档配置 cors ? :

http://vue-starter-webpack-cli-4-node.openode.io/

非常感谢!

编辑:这是我在询问我的cors配置时得到的内容,看来我还没有任何cors配置? :

C:\UwAmp\www\vue-starter-webpack-cli-4-node-gcloud>gsutil cors get gs://cedar-network-259109.appspot.com
gs://cedar-network-259109.appspot.com/ has no CORS configuration.

编辑2:

我正在向我的Google云应用发送CORS参数:

>gsutil cors set cors-json-file.json gs://cedar-network-259109.appspot.com

这是我的cors-json-file.json文件:

  [
    {
      "origin": ["https://cedar-network-259109.appspot.com"],
      "responseHeader": ["Content-Type"],
      "method": ["GET", "POST","HEAD", "DELETE"],
      "maxAgeSeconds": 3600
    },
    {
      "origin": ["https://cedar-network-259109.appspot.com:8080"],
      "responseHeader": ["Content-Type"],
      "method": ["GET", "POST","HEAD", "DELETE"],
      "maxAgeSeconds": 3600
    }
]

仍然存在错误:

POST https://cedar-network-259109.appspot.com:8080/getUsers
    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://cedar-network-259109.appspot.com:8080/getUsers. (Reason: CORS request did not succeed).

有我的信息:

>gsutil cors get  gs://cedar-network-259109.appspot.com
[{"maxAgeSeconds": 3600, "method": ["GET", "POST", "HEAD", "DELETE"], "origin": ["https://cedar-network-259109.appspot.com:8080"], "responseHeader": ["Content-Type"]}]

我的请求网址:https://cloud.google.com/storage/docs/configuring-cors标头:

Host: cedar-network-259109.appspot.com:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
Accept: application/json, text/plain, */*
Accept-Language: fr,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: https://cedar-network-259109.appspot.com/
Origin: https://cedar-network-259109.appspot.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Content-Length: 0

我的app.yaml文件:

runtime: nodejs
env: flex

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

编辑3:使用以下代码修改了我的节点app.js后端:

const cors = require("cors");
app.use(
  require("cors")({
    origin: function(origin, callback) {
      callback(null, origin);
    },
    credentials: true
  })
);

对此:

app.use(
  require("cors")({
    origin: "https://cedar-network-259109.appspot.com",
    credentials: true
  })
);

没有运气! 请帮助我

1 个答案:

答案 0 :(得分:0)

如果您尝试访问的资源存储在Google云端存储中,则您需要按照documentation you mention中的说明启用CORS。如果您尝试访问由其他Google App Engine应用程序托管的文件的资源,则需要按照documentation中的说明修改app.yaml文件。