简体   繁体   English

在 Github 页面上,您可以将 React 应用程序作为单个文件运行,并将其称为 function。在 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?

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.

相关问题 您可以从 Cloud Function 内部调用 Cloud Run 应用程序吗? - Can you call a Cloud Run app from inside a Cloud Function? 谷歌云,GitHub pipe 与谷歌云运行 - Google Cloud, GitHub pipe with Google Cloud Run 您可以在 Google Cloud Workflows 中运行 gsutil 吗? - Can you run gsutil in Google Cloud Workflows? 通过 Next.js(使用 Typescript)应用程序调用 Google Cloud Function 的正确方法是什么? - What Is The Right Way to Call A Google Cloud Function via A Next.js (with Typescript) App? Google Cloud Run 的默认存储空间是多少? - What's the default storage for Google Cloud Run? 如果您不想发送响应,在云功能中获取数据的最佳方式是什么? - what is the best way to get data inside a cloud-function in case you don't want to send a response? 从前端应用调用谷歌云 Function - Call Google Cloud Function from front-end application 如果 Google Cloud Platform 在 ADFS 上配置了 SAML,您能否使用 LDAP 连接将安全与 AD 集成? - If Google Cloud Platform is configured with SAML on ADFS, can you integrate security with just AD using an LDAP connection? 我无法使用 Visual Studio 2019 中的 Google Cloud Platform 工具将 App Engine 应用程序发布到 GCloud - I can't publish a App Engine application to GCloud using the Google Cloud Platform tools in Visual Studio 2019 如何在 python 程序中定义我的 Google Cloud Platform 服务帐户的密钥? - How do I define my Google Cloud Platform service account's key in a python program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM