在 Github 页面上,您可以 运行 将 React 应用程序作为单个文件并将其作为函数调用。在 Google Cloud Platform 上执行此操作的最佳方法是什么?

On Github Pages you can run a React application as a single file and call it as a function. What's the best way to do that on Google Cloud Platform?

我有一个启用了页面的 Github 存储库。在 Pages 中,我有一个包含函数的 index.js。当您点击 HTTP 端点时,该函数会自动 运行。该应用程序是一个小部件,所以我不想托管整个网站。我只是希望能够从 <script> 标记和 运行 我在第三方站点中的代码访问端点。

index.js 文件是通过使用 Parcel 构建我的 React 应用程序并将其编译为单个 js 文件而创建的。文件本身 运行 是一个 IIFE - 这是由 Parcel 在构建后创建的,看起来像这样:

!function(){function e(e,t,n,r){Object.defineProperty...
// hundreds of lines minified into one humongus line of code
...{domElement:e})}),e)})),Er()}();

作为参考,我按照 this tutorial, and you can see the author's script in their github repo here.

创建了它

但是 Github PAges 是 public,所以我想将我的脚本移动到 GCP。我以为一个简单的云函数就可以了,但我得到了可怕的 could not handle the request 错误。对于我的 Cloud Functions 应用程序,我尝试了以下操作:

exports.helloWorld = (req, res) => {
  const widget = // copy in all of the above code
  res.status(200).send(widget);
}

这引发了上述错误。

有没有一种 better/different 方法来托管像这样的单个函数脚本,在 http 调用中 运行s?

据我了解,您只是想在其他项目可以使用的地方托管一个 javascript 文件。如果您设置在 Google 云上,您可以将 javascript 文件上传到 public bucket。除非你想在每次请求文件时执行任何动态逻辑,否则你不需要比静态主机更复杂的东西。

您提到您想要使用 GCP,因为 Github 页面是 public,但您托管的任何文件都将是 public,除非您想要设置规则以便仅特定域可以请求该文件。