node.js - 谷歌云功能替换 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);
};
有人尝试将其部署在无服务器功能中吗?
解决方案
您正在使用req.url
其中包含请求 URL 的规范化版本。req.originalUrl
顾名思义,您需要使用保留原始请求的 URL。有关详细信息,请参阅Express 文档。
推荐阅读
- javascript - VUEX,$store.state 为空时怎么办
- angular8 - 同步融合中连接器的重叠
- mysql - 如何在 SQL 中多次关联表?
- python - Sympy solveset 将结果存储为浮点数、整数等
- excel - VBA 中的行数和列数显示不正确
- amazon-web-services - AWS Api Gateway 与 S3 作为 AWS 服务集成的 SAM 模板
- python - Python函数处理不存在的参数
- iframe - 包含带有 Web 扩展的 iframe 是否被视为远程代码?
- c# - 检查 Amazon S3 存储桶上是否存在文件夹
- javascript - 我正在使用 Chart.js 并尝试列出过去 7 天的出勤率