How to create Node HTTP server on Google Cloud Platform

This is a quick tutorial, beginner level, to get a nodejs serving to the web as fast as possible. The result will be that you can navigate to an ip:port on the web and get a http response.

Google Cloud Platform: https://cloud.google.com/compute/

Click Free Trial and sign up, it does require a credit card.

In the Google Developers Console, select compute Engine and click New instance.

Give it a Name.

On Boot Disk, click Change and select Ubuntu 14.04 LTS

Check the box for allow HTTP traffic.

There is a Networking tab, there is an option for creating a new static IP address. You can promote an ephemeral IP to static at a later time.

Create the instance and at the far right click SSH under Connect to open a console.

In the console:

~$ sudo apt-get update

~$ curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -

~$ sudo apt-get install -y nodejs

These instructions came from: NodeSource

Test that node is installed, type node. You should see a >. Type console.log(‘Hi’); You should see Hi. Then Ctrl+C to exit node, you’ll have to do it again after prompting.

Create a directory.

~$ mkdir myapp

Change to it.

~$ cd myapp

If linux and command line is new to you, a nice shortcut to know is that when typing a filename, if you press tab it will auto complete once it has enough letters to differentiate from other files.

I’m not a fan of editing files in the console, but vim is the answer to do it. You’ll need to search for some Vim tutorials, but I’ll give the needed commands for this simple tutorial.

To create a new file.

~$ vim hello.js

Now, you’ll be in the vim console. You can’t just edit from here as you’ll be in command mode to start.

Press i, now you should be in insert mode and can edit.

Type this in:

var http = require('http');
var server = http.createServer(function(req, res){
        res.writeHead(200);
        res.end('Hello Kitty');
});
server.listen(8080);

To run it:

~$ node hello.js

It will just sit there. Go to your Google Developers Console and click the SSH under connect for a new console and type:

~$ curl localhost:8080

You should see your message there, Hello Kitty

Now, this is almost there, but if you can’t navigate to this from the web yet. You need to enable the port for a tcp connection. To do this, in Google Developers Console select Firewall rules, it’s under Networking.

Click, New firewall rule.

Name it.

Under Source IP ranges, 0.0.0.0/0

Under Allowed protocols, tcp:8080

I put http-server under target tags, but it’s optional.

Now, if all went well you can navigate to the external IP and port to receive the http request and see the message in your browser. The external IP is to listed on the VM instances page of the Compute Engine. It’s to the left of the SSH button you’ve been clicking to open a console.

In a browser enter your external ip number followed by :8080 and Hello Kitty should show on the page.

That’s it for this tutorial. If you followed it your in a place now that you can start adding real features to your server. I’ll do more in this series to add to it.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *