【发布时间】:2019-11-19 15:03:42
【问题描述】:
我刚刚将 node.js - vue.2.0 应用部署到 Google Cloud。
vue.js 2.0 应用程序位于公共节点目录中。一切运行良好,除了我对 Web 服务的所有查询都失败了,在习惯性的 app.js 节点文件中,你知道.. 没有调用任何其他节点服务器,这就是为什么我什至尝试过 localhost:8080作为服务器名称。
https://cedar-network-259109.appspot.com
从应用完成的所有后端查询都失败了:
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).
我还管理应用程序调用谷歌云服务器 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).
当部署在 opennode.io 主机上时,此应用可以正常运行:http://vue-starter-webpack-cli-4-node.openode.io/。
MongoDb Atlas 已连接(0.0.0.0 通配符)。
问题: 我是否必须使用这个奇怪的命令在我的谷歌云应用程序上配置 cors ? :
https://cloud.google.com/storage/docs/configuring-cors
非常感谢!
编辑:这是我在询问我的 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:
我正在向我的谷歌云应用发送 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"]}]
还有我的请求 URL:https://cedar-network-259109.appspot.com:8080/getUsers 标头:
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: 使用以下代码修改了我的 node 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
})
);
没有运气! 请帮帮我!
【问题讨论】:
标签: node.js google-cloud-platform vuejs2 cors