Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Getting a function from an express server and running it in a webpage via a script tag

How do you return a function from an express server and run it in a browser?

I have hosted a function on Google Cloud Platform (Cloud Functions) and want to run it in a web application via a script tag. The function is in Nodejs 16 and can be called via https, and is a simple hello world example:

exports.helloWorld = () => {
  const widget = console.log("This is a widget");
  return widget();
}

However, it seems as though I cannot call it that way, because I get an Error: could not handle the request and a 500 status message each time I try to hit the endpoint.

I want to be able to embed this function on a website via something like:

<script src="https://example.com/function-name"/>

How can I achieve that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems that your Cloud Function is malformed. When you create a Cloud Function you have to return a status code otherwise the Cloud Function will timeout waiting for the return status code (throwing the 500 error you had).

If I guess correctly what you want to do is call an external script from the Cloud Function, you can use the next code:

exports.helloWorld = (req, res) => {
  let message = 'alert("Hello world from CF")'; //Or any js code script here
  res.status(200).send(message);
};

And call it from the HTML page like this:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://zone-project-id.cloudfunctions.net/helloworld"></script>
    </head>
    <body>
        <!-- the content goes here -->
        Hello world!
    </body>
</html>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!