[英]Enable Cors on Google Compute Engine
I'm deploying a node.js API, and a website on Google Compute Engine, every thing works fine on my local machine, also they both work well separately, but whenever i call my API i receive the following:我正在部署 node.js API 和 Google Compute Engine 上的网站,一切都在我的本地机器上运行良好,它们也分别运行良好,但每当我打电话给我的 ZDB974238714CA8DE634A7CE1D083A14F 时,我都会收到以下信息:
Access to XMLHttpRequest at 'my-api-domain' from origin 'my-website-domain' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
i use express and i use cors middleware to handle CORS, and it works on my machine, but it seems google disable this kind of response headers.我使用 express 并使用 cors 中间件来处理 CORS,它可以在我的机器上运行,但似乎谷歌禁用了这种响应头。
my express setup我的快速设置
const express = require('express')
const bodyParser = require('body-parser')
const user = require('../routes/user')
const cors = require('cors')
module.exports = function(app) {
app.use(cors())
app.use(express.json())
app.use( bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
}
Note: I tried to send a get request to the api from the browser and it works and i recieve access-control-allow-origin: *
in the response headers!注意:我尝试从浏览器向 api 发送获取请求,它可以正常工作,并且我在响应标头中收到access-control-allow-origin: *
!
but when i call the same endpoint from the websites's origin these headers are not being sent.但是当我从网站的来源调用相同的端点时,这些标头不会被发送。
The error message is related to CORS and it shows that the process was not complete when a browser makes a simple request to Cloud Storage.该错误消息与 CORS 有关,它表明当浏览器向 Cloud Storage 发出简单请求时该过程未完成。
Also I mentioned that to handle HTTP, Cloud Functions uses a particular HTTP framework(Express 4.16.3) in Node.js (6, 8, & 10) runtime.我还提到要处理 HTTP, Cloud Functions 在 Node.js(6、8 和 10)运行时中使用特定的 HTTP 框架(Express 4.16.3)。
If you answer the questions by guillaume blaquiere & sideshowbarker, we are able to better support.如果您回答 guillaume blaquiere 和 sideshowbarker 的问题,我们能够提供更好的支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.