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?
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.