在github页面上,您可以将React应用程序作为一个文件运行,并将其称为函数。在Google Cloud平台上做到这一点的最佳方法是什么?

发布于 2025-01-21 06:40:35 字数 1131 浏览 3 评论 0原文

我有一个启用页面的github存储库。在页面中,我有一个包含函数的index.js。当您击中HTTP端点时,该功能会自动运行。该应用程序是一个小部件,因此并不是我想托管整个网站。我希望能够从< script>标记中登录一个端点,并在第三方网站上运行我的代码。

index.js文件是通过用包裹构建我的React应用程序并将其编译到单个JS文件来创建的。该文件本身运行了一个IIFE-这是由包裹创建后的,看起来像这样的东西:

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

为参考,我通过关注本教程,您可以在其github repo 在这里

但是GitHub页面是公开的,所以我想将脚本移至GCP。我认为一个简单的云功能可以做到,但是我得到可怕的无法处理请求错误。对于我的云功能应用程序,我尝试了以下操作:

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

这引发了上述错误。

是否有更好/不同的方法可以托管在HTTP调用上运行的单个函数脚本?

I have a Github repo that has Pages enabled. In Pages, I have an index.js that contains a function. The function is run automatically when you hit an HTTP endpoint. The application is a widget, so it's not like I want to host a whole website. I jsut want to be able to hit an endpoint from a <script> tag and run my code in a third-party site.

The index.js file is created by building my React app with Parcel and compiling it down to a single js file. The file itself runs an IIFE - this is created by Parcel after building and looks something like this:

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

For reference, I created it by following this tutorial, and you can see the author's script in their github repo here.

But Github PAges is public, so I wanted to move my script to GCP. I thought a simple Cloud function would do, but I get the dreaded could not handle the request error. For my Cloud Functions app I tried the following:

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

This threw the above error.

Is there a better/different way to host a single function script like this that runs on http call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟 2025-01-28 06:40:35

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

您提到要使用GCP,因为GitHub页面是公开的,但是您主机的任何文件都会公开,除非您要设置规则,以便只有特定域才能请求该文件。

From what I understand, you're simply trying to host a javascript file somewhere where it can be used by other projects. If you're set on Google cloud, you could just upload your javascript file to a public bucket. Unless you want to perform any dynamic logic each time the file is requested, you don't need anything more complicated than a static host.

You mention that you want to use GCP because Github pages is public, but any file you host would be public unless you want to set up rules so that only specific domains can request the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文