Push your first App into the Cloud (Node.js)

Deploy your first Node.js App into the cloud - easy beginners guide to start

We would like to show you how you can easily push a new Application into the Cloud. We selected Node.js as a programming language in this blog, because there is a big hype right now around this language. More info about the popularity of Node.js: https://www.airpair.com/javascript/node-js-tutorial

Prerequisite for this blog: Some basic Developer know-how

Preparation

To get started, you need some tools on your local machine. The correct node runtime as well as the latest Cloud Foundry CLI to login into the Cloud:

  • Node Runtime
    Go to https://nodejs.org/en/download/ and download and install the LTS version of Node.js for your local machine. Now you’ve got a local node runtime; if you open the command line and type

    node –v (or node --version on windows)

    and you should see the Node.js version you have installed.

  • CloudFoundry CLI
    The Cloud Foundry Command Line Interface (CF CLI) is a command line-tool for deploying and managing your applications. It can be used in combination with the Developer Console since a lot of the functionality is covered by both interfaces. You can download the CF CLI from https://github.com/cloudfoundry/cli/releases/ .
    Download the installer for your OS and try the command cf help to check whether the tool installed correctly.
    (check www.cloudfoundry.org to find out more about CloudFoundry and why Swisscom is supporting this Foundation)

First local Node.js appnodejs-logo

As soon as you have the tools ready, we can start writing our first Node.js App – wohoo!

Therefore, create a new file on your local machine named: app.js with the following content:

 

 

var express = require('express'); // Webframework for nodejs, quite powerfull
var app = express(); 

app.get('/', function (req, res) { // telling nodeJs to get all commands from / into this function
  res.send('Hello World!'); // give a Hello World back, if a request comes in 
});


var port = process.env.PORT || 3000 // either use the port 3000 or a port which is in the "environment variable" - the cloud will deliver us such a port 
app.listen(port); // tell nodejs to listen to this port and give response

console.log('I am ready and listening on %d', port); // write something nice in the console, that the developer sees, it works

 

 

In the comments you can see what each line is functionally representing in this script.
Node.js needs to know what kind of modules you’re using, so that it can prepare the deployment. Therefore we need to create a new package.json file with the following content:

{
"name":"nodejs123-example",
"version":"0.0.1",
"author": "Demo",
"dependencies": {
 "express": "4.13.3"
 },
"scripts": {
 "start": "node app.js"
 }
}

 

Now we’re ready for a first test – both files should be in the same folder. Start your command line and jump into the directory in which you just created the two files and do a:

npm install

 

This will download all dependencies (i.e. express for example) and make sure that they’re ready to start.
Now we start the app with:

node app.js

 

Starting your local Node.js app the first time!

The command line tells us, that the app is now running on port 3000! So lets check it out and access http://localhost:3000 on your machine.

Does it work ?
Eh voilà !

step2



Move into the Cloud

The next step is to deploy you App and run it in the Cloud. For that you need a Swisscom Developer Account.
Go to https://console.developer.swisscom.com/ and register yourself as a new user. After that you must enter a credit card (hint: consumption up to 100 CHF will be covered by Swisscom in the first 3 monthly bills!). After that, create a “new Organisation” as described in the wizard. An Organisation will allow you to invite other Developers into your environment and jointly work on a project. After that, create a Space. A Space allows you to separate nicely between development/quality/production environment in the future.

As you’re now registered, lets go back to your command line and log in to the Cloud:

cf login -a https://api.lyra-836.appcloud.swisscom.com

 

After typing in your user/pw you should now be ready to deploy something. Lets start easy and just deploy your app:

cf push myApp<YOURNUMBER>

 

If that was successful, you can directly test your app here: http://myapp<YOURNUMBER>.scapp.io/
That was simple wasn’t it ?


 

Scaling your app

Lets get a little bit more sophisticated and try to duplicate your app – i.e. scale it up.
With the following command you can find out how your app is doing:

cf apps

And with a simple call you can scale your app from 1 to 4 – just with one command! (with automated load balancing in the back)

cf scale myApp<YOURNUMBER> -i 4

(with –i you can specify the number of App instances)
And then double check again how your app is doing with

cf apps

Congrats, you deployed your first Node.js app in the Cloud and scaled it up!


 

Combine Services

You can find more details on how to use Databases and how to attach them to your App in our Documentation; https://docs.developer.swisscom.com/services/services/managing-services.html


 

What’s Cloud Foundry ?

Cloud Foundry is a Open Source and defacto-industry Standard Platform as a Service Project with a huge adoption in the Market. Swisscom is an early adopter and Gold Member of the Cloud Foundry Foundation and is heavily investing into Open Source . You can find more information on my last blog;

or on the official Cloud Foundry Foundation Website:

 

We hope you enjoyed this short “Beginners guide”; stay tune for more.