Developer Perspectives: “Hello, World” in Express Serverless Platform vs Google Cloud Functions

Google Cloud Functions are Google Cloud’s equivalent to AWS Lambda. Like Lambda, Google Cloud Functions let you write custom functions and expose them via an HTTP interface through either Google Cloud Function’s minimal HTTP configuration or Google Cloud Endpoints for more sophisticated use cases. LunchBadger offers Express Serverless Platfom,  a similar stack that lets you write Node.js APIs and expose those APIs through Express Gateway without vendor lock in. In this article, I’ll walk through setting up a rudimentary Node.js function in both LunchBadger and Google Cloud, and discuss the tradeoffs of using one over the other.

Get Started with Google Cloud Functions

Go to the Google Cloud Functions landing page and click “Try it free”. Google Cloud offers a free trial with $300 in credits for 1 year, which should be more than enough for this simple walkthrough.

 

 

Click on the hamburger icon in the upper left and find the “Cloud Functions” link in the sidebar, then click “Create function”.

 

Name your function “hello-world” and leave the rest of the options in the “Create function” form unchanged. In particular, leave “Function to execute” as “helloWorld”, because that needs to match the name of the function your source code exports. Enter the below code as the source code for your function to print out the version of Node.js your cloud function is running on.

 

 exports.helloWorld = (req, res) => {
 res.send('Hello from Node.js ' + process.version);
 };

 

 

Click “Create” and wait for Google to deploy your cloud function. It will take a few minutes, so you might take this time to check out Google’s cloud functions emulator that lets you run cloud functions locally and deploy them from the command line.

Once your function is deployed, click on it to display the function’s details.

 

 

Click the “Trigger” tab to find your cloud function’s URL.

 

 

Copy the URL and use curl to run your cloud function.

 

 $ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world
 Hello from Node.js v6.11.5
 $

Google Cloud Functions don’t give you any control over what version of Node.js you run, they run Node.js v6.11.5 currently and will upgrade when they upgrade. Google’s stated policy is to follow the Node.js LTS release schedule, but as of this writing they have not upgraded to Node.js 8.x despite it being an LTS release for 6 months.

Express Serverless Platform and Express Gateway

In Express Serverless Platform, first create a new function and name it ‘myfunction’:

 

 

Node.js functions in Express Serverless Platform have the same function signature as native Node.js HTTP request listeners. The function to print ‘Hello, world!’ with Express Serverless Platform is shown below. Note that the exported function **must** have the same name as the function name you assigned in the LunchBadger GUI.

 

 module.exports = {
 myfunction (req, res) {
 res.end('Hello, world!');
 }
 };

 

 

Note that this function does not have to create an actual HTTP server. Express Serverless Platform handles creating a server for you, so you can write “serverless” functions without having to actually write an Express.js application. You still get access to the Node.js HTTP response, so you can set HTTP headers and set the HTTP status code.

From the menu on the left, add a new gateway. This will add an Express Gateway instance to your project.

 

 

Click the plus icon next to “Policies” to add a policy to the default Express Gateway pipeline. In Express Gateway, a pipeline is a sequence of policies for handling a request. Add an empty ‘proxy’ pipeline.

 

 

Drag a line from your gateway to your function, and Express Serverless Platform will automatically connect your proxy policy to your function.

 

 

Express Serverless Platform will also create a function endpoint for you. Keep the “paths” for your function endpoint empty for now.

Click on the “settings” icon to find the publicly accessible URL for your Express Serverless Platform project.

 

Running curl on the URL will now run your custom function and give you the “Hello, world!” text.

 

 $ curl http://gateway-148-dev.lunchbadger.io
 Hello, World!
 $

Moving On

The combination of Express Serverless Platform and Express Gateway is a great alternative to Google Cloud Functions if you’re looking to avoid vendor lock-in. The Google Cloud Function emulator lets you develop locally and deploy to Google Cloud, but it only supports invoking functions from the command line, so running a production application on the emulator isn’t a viable option. On the other hand,Express Serverless Platform scaffolds out an app for you and stores it in a git repo that you can clone. That enables you to run your app locally or on any cloud provider.Express Serverless Platform does automatically host your app for you, but that isn’t your only option. Google Cloud is appealing because Google is a well-established company with a proven track record of running sophisticated developer tools at massive scale. But using Google Cloud Functions means you must run your code on Google Cloud and manage your API using Google Cloud Endpoints. Furthermore, Google Cloud Functions have slightly different syntax than Azure Functions or IBM Cloud, so switching cloud function providers requires significant refactoring.

Additionally, if you’re interested in more of these topics, join the live discussion on twitter (@lunchbadger) or (@express_gateway).


Next Post