Advertisements

How to run your Node.js Express app with Google Firebase Functions

Google Cloud Functions is a serverless, Node.js environment that does not require any administration and is easy to use. You need a Google Cloud Platform account or project, to run your node.js express app on Google Functions.

Google Cloud Functions for Firebase is this Firebase layer on top of Cloud Functions. So it’s still Cloud Functions, the Cloud product.

What can you do with Google Firebase Cloud Functions, many things one option that you have is, if you want to respond to events that happened in your Firebase ecosystem, you can write a bit of code and publish it to Google Firebase Functions. If something changes in your database, a user logs, you can respond to these events in your web or/and mobile apps.

Google Firebase Cloud Functions are single-purpose JavaScript functions that are executed in a secure, managed Node.js environment. They are only executed when a specific event being watched is emitted.

Before you can deploy an application to Google Cloud Functions, you have to have something to deploy. Any Node.js application can be deployed. Lots of developers like Express, so let’s use it. If you aren’t familiar with Express, it’s like Flask in Python or ASP.NET in C#. More information can be found here: https://expressjs.com/

Deploy Express app

To deploy your Express app, you need to create one more JavaScript file and run one command to deploy your app to Cloud Functions.

Cloud Functions expects to see a file called index.js, and inside that file, the Express app is using app.js as the app starting point. You need to add an index.js file and add definition to launch the Express app.

These are the steps to take.

Step 1

Add a new file called index.js and paste the code below in that file.

var app = require('./app');

function App(req,res) {
   if (!req.url) {
       req.url = '/';
       req.path = '/';
   }
   return app(req,res);
}

var twittersentiment = App;

module.exports = {
   converter
};

Code explained

The first line imports the Express app module. The function App() uses a slash (/) at the end of the request, as it turns out that the Cloud Function won’t work if there isn’t a slash (/) at the end of the request. The function just makes sure that if there is no slash, one is added and the function just returns the app module imported on the first line.

The line that reads:

var wittersentiment = App; 

just sets the name of the function and must match the name of the function that is exported.

In the last line, the converter function is exported.

Step 2

To deploy your express app, use the gcloud command

gcloud beta functions deploy twittersentiment --trigger-http

It will take a minute or two for this command to complete. The name of your Cloud Function will be twittersentiment.

Every Cloud Function needs a trigger. There are different triggers; for example, a change in a Cloud Storage bucket could be a trigger or a Pub/Sub message could be a trigger.

In this case, the trigger is an HTTP request.

After you pushed your Express App to the Google Cloud Firebase Function, you can check that your function is available, go to the Firebase console, select functions and you should see your function listed.

Step 3

As we created an HTTP trigger for the twittersentiment function, open the URL listed for your functions that should start your function. You can check in the logs to see if your Express app is executing.

Google Firebase Cloud Function Logs

Conclusion

Google Firebase Cloud Functions is a way of writing server code without having to have a server. Google Firebase Cloud Functions are single-purpose JavaScript functions that are executed in a secure, managed Node.js environment. They are only executed when a specific event being watched is emitted.

Deploying the code to Google Firebase Cloud Functions requires just one command. After that, Cloud Functions automatically scales up computing resources to match the usage patterns of your app. You never worry about SSH credentials, server configuration, provisioning new servers, or decommissioning old ones.

The benefit of using Google Cloud Functions is not only that you don’t need to maintain a server, but it’s also that application logic is best controlled on a server to avoid anyone to tamper with it on the web or mobile client side. Google Firebase Cloud Functions is fully insulated from the web or/and mobile client so you can be sure that your functions are private and secure and can’t be reverse engineered.
Here you can read more about – Google Firebase Cloud Functions.

Advertisements

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

Subscribe for updates

Enter your email address to subscribe to receive notifications of new content by email.

Join 5,802 other subscribers

Advertisements