[英]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?
I have a Github repo that has Pages enabled.我有一个启用了页面的 Github 存储库。 In Pages, I have an
index.js
that contains a function. The function is run automatically when you hit an HTTP endpoint.在页面中,我有一个包含 function 的
index.js
。当您点击 HTTP 端点时,function 会自动运行。 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.我只是希望能够从
<script>
标记中访问端点并在第三方站点中运行我的代码。
The index.js
file is created by building my React app with Parcel and compiling it down to a single js file. index.js
文件是通过使用 Parcel 构建我的 React 应用程序并将其编译为单个 js 文件来创建的。 The file itself runs an IIFE - this is created by Parcel after building and looks something like this:该文件本身运行一个 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()}();
For reference, I created it by following this tutorial , and you can see the author's script in their github repo here .作为参考,我是按照本教程创建的,您可以在此处的 github 存储库中查看作者的脚本。
But Github PAges is public, so I wanted to move my script to GCP.但是 Github PAges 是公开的,所以我想将我的脚本移动到 GCP。 I thought a simple Cloud function would do, but I get the dreaded
could not handle the request
error.我以为一个简单的云 function 就可以了,但我得到了可怕的
could not handle the request
错误。 For my Cloud Functions app I tried the following:对于我的 Cloud Functions 应用程序,我尝试了以下操作:
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?是否有更好/不同的方式来托管像这样在 http 调用上运行的单个 function 脚本?
From what I understand, you're simply trying to host a javascript file somewhere where it can be used by other projects.据我了解,您只是想在其他项目可以使用的地方托管一个 javascript 文件。 If you're set on Google cloud, you could just upload your javascript file to a public bucket .
如果您设置在 Google 云上,则只需将 javascript 文件上传到公共存储桶。 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.
除非您想在每次请求文件时执行任何动态逻辑,否则您不需要比 static 主机更复杂的东西。
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.你提到你想使用 GCP,因为 Github 页面是公开的,但你托管的任何文件都是公开的,除非你想设置规则,以便只有特定的域可以请求该文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.