NodeJS on Google App Engine
Are you looking for free hosting for your node projects? You could try DigitalOcean but GAE Managed VMs are now in Beta (you can use them without whitelisting) so I wanted to dedicate a quick post on running Node on Google App Engine. It could be a free and easy way to host your node.JS app. Since we are not coding in Java, Python, or Go Lang, we need to setup a custom runtime environment on a Virtual Machine. So, below I will go over installing Managed VMs with Google Cloud SDK and Docker to build and deploy a sample app, written in Node and Express, to Google App Engine.
Step 1: Install the Google Cloud SDK
To setup gcloud on OSX, we can use the following one-liner:
curl https://dl.google.com/dl/cloudsdk/release/install_google_cloud_sdk.bash | bash ; unzip google-cloud-sdk.zip ; ./google-cloud-sdk/install.sh
If the install.sh fails to be found, just go to google-cloud-sdk directory and run install.sh there. Then run gcloud auth login, accept the permissions, and give your project a name: gcloud config set project node-ip.
I named my project node-ip because my simple node.js app will be showing an end user’s IP address. Since we want gcloud to preview and run the app, lets get those components with gcloud components update preview app
Step 2: Install Boot2Docker for Managed VMs
The Google Cloud SDK relies on Docker to configure and build Linux containers that Managed VMs can use to build locally and deploy custom runtime apps to the App Engine. Boot2Docker is a lightweight utility that installs a virtual machine (via VirtualBox) to run the Docker daemon and thus, it allow you to mimic the production environment locally. Managed VMs currently support boot2docker version 1.3.0 so I grabbed that and ran the installer.
After the install, I ran the Boot2Docker app from the Applications folder which opened a terminal window. In the terminal, I ran $(boot2docker shellinit) to set the DOCKER_HOST, DOCKER_CERT_PATH and DOCKER_TLS_VERIFY environment variables.
Step 3: Run Node.JS with gcloud and Docker
This article is not really about writing node apps so I grabbed a quickstart node app from GitHub and made some changes to it. I removed the hello route and added an ip route which responds with the IP address from the HTTP request via req.connection.remoteAddress. I also updated the static html file to load Bootstrap, jQuery, and show a link to the ip page which gets queried with an Ajax call. Finally, in the app.yaml file I removed “login:required” and changed app.js script to server.js.
To preview locally, I ran gcloud preview app run . in the folder where app.yaml exists. It build the app and I could go to localhost:8080 in my browser and preview the application. It did show my my IP address. Now let’s deploy to Google App Engine.
Step 4: Deploying NodeJS App to Google App Engine
First, I went to my App Engine Console and created the node-ip app. Managed VM hosting uses Compute Engine Pricing for each VM so I had to enable billing for the app but I was able to start a 60-day free trail with $300 credit. That’s great and here is the command to deploy my node app to GAE:
gcloud –project node-ip preview app deploy –server preview.appengine.google.com .
That almost worked. There was an error (Image with tag google/docker-registry was not found) which required me to pull the google/docker-registry image (docker pull google/docker-registry) and after that finished, I ran the deploy command again and everything was successful.
Now, you can checkout my Simple “Find my IP” nodejs app on Google App Engine.
It’s almost perfect… The only problem is that the IP is not my IP but some link-local address that belongs to a private network in California. I assume it’s the VM’s IP, no?
Uninstall docker from OSX
Utility library for accessing App Engine services using Node.js
Simple file app with Node and Express
Build a RESTful API with Node and Express