Google云功能取代了网址中的双斜杠

时间:2019-07-11 17:43:10

标签: node.js google-cloud-platform cors google-cloud-functions cors-anywhere

我正在尝试在Google Cloud Functions上部署cors-anywhere。我应该在gcp的链接后提供网址。

它看起来像这样:

https://us-central1-my-project.cloudfunctions.net/my-function / http://dummy.restapiexample.com/api/v1/employees

但已转换为:

https://us-central1-my-project.cloudfunctions.net/my-function /http:/dummy.restapiexample.com/api/v1/employees

将主机后的所有双斜杠转换为简单的双斜杠。

我尝试替换 req.url http:/ 转换为 http:// ,但仍然无法正常工作。也许需要在网络服务器级别对此进行修复。

这是我在GCP中的职能

var cors_proxy = require('cors-anywhere').createServer({
  requireHeader: ['origin', 'x-requested-with'],
  removeHeaders: [
    'cookie',
    'cookie2',
  ],
  // See README.md for other options
});

exports.myFunction = (req, res) => {
  req.url = req.url.replace('/my-function/', '/'); // Strip '/my-function' from the front of the URL, else the proxy won't work.

  return cors_proxy.emit('request', req, res);
};

有人试图在无服务器功能中部署它吗?

1 个答案:

答案 0 :(得分:0)

您正在使用req.url,其中包含请求URL的规范化版本。您将要使用req.originalUrl,顾名思义,它保留了原始请求的URL。有关更多信息,请参见Express docs