deploy hello world in a google cloud function in javascript

To deploy a "hello world" function to Google Cloud Functions:

  1. Create a new directory for your project and navigate into it:
index.tsx
mkdir my-cloud-function
cd my-cloud-function
45 chars
3 lines
  1. Initialize your project with npm:
index.tsx
npm init -y
12 chars
2 lines
  1. Install the Google Cloud Functions SDK:
index.tsx
npm install @google-cloud/functions
36 chars
2 lines
  1. Create a new file called index.js and add the following code to it:
index.tsx
exports.helloWorld = (req, res) => {
  res.send('Hello, World!');
};
69 chars
4 lines

This defines an HTTP-triggered function that responds with "Hello, World!" when it is called.

  1. Create a package.json file that specifies the name of your function and its entry point:
{
  "name": "my-cloud-function",
  "version": "1.0.0",
  "main": "index.js"
}
78 chars
6 lines
  1. Deploy your function to Google Cloud Functions:
index.tsx
gcloud functions deploy helloWorld --runtime nodejs12 --trigger-http
69 chars
2 lines

This command deploys your helloWorld function to Google Cloud Functions with the nodejs12 runtime and an HTTP trigger. You should see a message indicating that your function was successfully deployed, along with its URL.

  1. Call your function using its URL:
index.tsx
curl <your-function-url>
25 chars
2 lines

This should return a response of "Hello, World!".

Congratulations, you've deployed your first Cloud Function!

gistlibby LogSnag