首页 > 解决方案 > 谷歌云功能替换 url 中的双斜杠

问题描述

我试图在 Google Cloud Functions 上部署cors-anywhere 。我应该在 gcp 的链接之后提供 url。

它看起来像这样:

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);
};

有人尝试将其部署在无服务器功能中吗?

标签: node.jsgoogle-cloud-platformcorsgoogle-cloud-functionscors-anywhere

解决方案


您正在使用req.url其中包含请求 URL 的规范化版本。req.originalUrl顾名思义,您需要使用保留原始请求的 URL。有关详细信息,请参阅Express 文档


推荐阅读