Create Google Cloud Functions using Node.js

In this blog, let’s us see how to create google cloud functions using node.js in step by step

This image is taken from Google Cloud

Before creating a cloud function, we have to set up a firebase project in our local machine.
Click here to see my previous article which explained in detail how to set up a firebase project.

Look at the below screenshot which is my project directory

In the index.js file, we can see commented lines of code which has generated after step up the project.

We can un-comment these lines or can create our own custom function.

Before creating a cloud function, we must know what is cloud function and why it is often used in many cloud-based architectures.

Cloud Function — is a serverless compute which has a block of code that can be executed in the cloud platform.

This statement can be applied to any cloud platform like Azure or Google or AWS etc..,

In Google — it is known as Google Cloud Function
In MS Azure — it is known as Azure Function
In AWS — it is known as Lamba Function

In this article, we are going to see on Google Cloud Function. We can also see other cloud platforms in my different articles.

  1. Language-Independent — we can choose our convenient languages like Node.js, Python, C#, PowerShell, Java etc..,
  2. Executable — it is easy to deploy the code in the cloud and accessible from anywhere
  3. Types of Functions — it has various triggers like HttpTrigger, Cloud Storage, Cloud Pub/Sub, Cloud Firestore, etc.., Click here to view in details

Now, let’s see on how to create HttpTrigger function:

After setting up the firebase project, we can create the function in index.js file

I just un-commented the code lines from helloWorld function like this:

  1. const functions = require(‘firebase-functions’);

2. exports.helloWorld = functions.https.onRequest((request, response) => {

3.“Hello logs!”, {structuredData: true});

4. response.send(“Hello from Firebase!”);


In any HttpTrigger cloud function, we have to return the value from a function through HttpResponse object.

We can execute the function locally before actual deployment.
Type this command in VSCode Terminal or PowerShell Window or Command Prompt
firebase serve — only functions (this command will serve at local and provide endpoint URI like this)


To test this endpoint URI, we can use Postman API tester and it is my favorite API test tool.

Look at above screenshot in postman and this function returns as “Hello from Firebase!”

Let’s see how it returns from function in code line by line
1. const functions = require(‘firebase-functions’);
(The above line of code is importing “functions” object as constant from the “firebase-functions” library.
In Node.js, we have to use the keyword “require” to import any library.

2. exports.helloWorld = functions.https.onRequest((request, response) => {
ëxports.helloWorld — is creating the function name as “helloWorld” with keyword exports (In node.js, if we use exports as the keyword used to expose our module — to know about in detail, click here).

functions.https.onRequest((request, response) => {
This code specifically mentioning this function is executable while http request which is nothing but HttpTrigger

Note : there are two arguments request and response which is nothing but HttpRequest and HttpResponse.

request — holds the paramenter from http request and other request properties like method types “GET”, or “POST”, or “PUT” etc.., request origins and more.

response — can return the value from functions, and returns HTTP response object with properties like HTTP Status such as status code

We have to return the value from a function using a response object only.

3.“Hello logs!”, {structuredData: true}); — this line of code is used for logging mechanism and trace the functions for development purpose

4. response.send(“Hello from Firebase!”); — this is returning string from this function.

We can return the value from a function in any format like as string, number, or any object (json) as well.

To deploy this function at our respective cloud environment — type this command
firebase deploy — only functions (this command will deploy all functions from this current file to our project in the cloud)

If we want to deploy particular function then use this below command
firebase deploy — only functions:helloWorld (this command will deploy “helloworld” function only in the cloud)

That’s all. now we have created cloud function successfully.

Happy Learning!!!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store