DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Mixing Google Cloud Functions and Express

Mixing Google Cloud Functions and Express

Let's split an Express handler into individual functions, which we'll run via Google Cloud Functions, working around the deployment-time kinks along the way.

Mateusz Haligowski user avatar by
Mateusz Haligowski
·
May. 19, 17 · Tutorial
Like (3)
Save
Tweet
Share
4.87K Views

Join the DZone community and get the full member experience.

Join For Free

Google Cloud Functions are an awesome tool, which I have already described a couple of times in other posts. If you look at the functions triggered by HTTP calls, they turn out to be regular Express handlers. There are probably some differences in the internals, but that’s not something that would drastically change how you write your functions.

So, the question is: Can you run the functions written with Cloud Functions in mind with an Express application? Or, the other way around: Can you split your Express application to separate Cloud Functions? This question may sound like a pure experiment, but I can imagine a situation where this could be useful. You may have already an application running in an App Engine instance, but you don’t want to handle that anymore — splitting into a bunch of Cloud Function makes perfect sense.

Let’s assume we have a very simple application, with a single handler. All the handler does is return a piece of text:

// hello.js
module.exports = function hello(req, res) {
    res.send('Hello');
}


And the code to serve that is regular Express boilerplate:

// server.js
var express = require('express');
var app = express();

var hello = require('./hello.js');

app.get('/hello', hello);

app.listen(3000, function() {
    console.log('Listening...');
});


Nothing unusual for now. You can run the code in an App Engine environment, you can create a Docker container out of it, but also you can create a Cloud Function, without getting rid of the Express part at the same time. This is very useful for larger refactorings, when you want to maintain the same functionality until the new dependency is resolved everywhere.

Here’s the thing about Cloud Functions. In order to run properly, it requires an index.js file or function.js file in the main folder that’s deployed. We don’t want to change anything in the current structure of the application, so here’s a simple workaround:

// function.js
exports.hello = require('./hello');


The bottom line of this is we make our handler available as an export so that Cloud Functions can pick it up at deployment time. And so, to deploy the function you only need the specify the name of the function, and it needs to be identical with the exported name in the function.js file:

$ gcloud beta functions deploy hello 
--entry-point hello 
--trigger-http 
... other parameters


And that's it! You successfully converted a configured Express handler into a Cloud Function!

As you have probably noticed, there are some caveats with this. Cloud Functions don’t have the router included, so all the calls are being targeted to a specified address. That can pose a problem when your handler takes only specific HTTP methods — for example, using it to read values from a form on your website.

Another drawback is that you need to add every single exported function in the function.js file. Usually, it shouldn’t be a problem, but when you have many functions to deploy, just going through them may be troublesome. Not to mention that you have to deploy your full application to the Cloud Functions!

All the files are available as a Gist. Feel free to fiddle with them!

Express Cloud Google (verb)

Published at DZone with permission of Mateusz Haligowski. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Introduction to Spring Cloud Kubernetes
  • REST vs. Messaging for Microservices
  • Solving the Kubernetes Security Puzzle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: