Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Whats the best way to implement an NodeJS Express API on GCP

Hello, 

First, I am very sorry if this is not the right place to ask this questions. 

I have build a little API (around 50 routes), using Nodejs/express. Following some tutorials, I used  google functions to directly deploy it on the cloud. 

But now that I start having a bit of traffic, few hundreds to thousands per days. I feel that I don't have a lot of control over it. On latencies and routes usage monitoring especially.

My entire API is basically one big Google function, I think it is not the best way to do it.

I did some research, but all tutorials seems to do that, deploy their APIs express API as one function. 

I tried to see if I could use Cloud Endpoints, but I am not sure if this what will solve my problems. 

So my question is this : 

"

Is deploying an entire Nodejs/express API as just one google function is the best way to do it ? 

If not then whats the best way ? Each route as a separate functions ? 

"

0 2 737
2 REPLIES 2

Hi Martin,

Instead of invoking a Cloud Function directly, you can deploy your API on Cloud Run. You may also Set up Cloud Endpoints OpenAPI for Cloud Run with ESPv2. I hope this information points you in the right direction.

jkg
Google Developer Expert
Google Developer Expert

Hey Martin,

My suggestion to you would be to go with whatever your comfortable in.

At this point you have two options if you'd like to remain serverless.

  1. You can split your app out into a separate function for each route in your api, and may wish to split out further to a verb per route.
  2. You can make use of cloud run which would be to bundle up your entire node app into a container and then deploy it.

Personally, I have done both and there are advantages and disadvantages. A project I worked on did option 1 and it was easy to build and deploy separate routes quite easily without affecting code in the other parts of the app, meaning you can independently update those functions.

The second option kind of takes the monolith approach, and some applications such as ruby on rails or django apps can use cloud run quite effectively for this.

Given you're already using functions, my recommendation would be to stick with it, and perhaps test the waters a bit by creating a subsection of your app into separate functions per route/http verb and see how you go. If it works for you, then continue splitting it out.

Cheers!

JK