Archive

Getting started with IBM Bluemix and Node.js

Share this post:

If you’re like me when using a new system, it’s nice to have the simplest conceivable example to try out first and then play around a bit to make sure you’re in the driver’s seat. For the brand new IBM Bluemix user, here’s my attempt with no frills and no philosophy—just a very small amount of code and how-to. There are some wonderful directions on the fabulous Bluemix web UI available on the Bluemix website.

What you’ll be doing

You’re going to sign up to get an IBM ID, sign up for Bluemix, then run through a few simple commands to get your first piece of code up and running. This little run-through will use only the tiniest corner of the power of Bluemix—but every journey starts with a first step.

Micro-primer on Bluemix

Bluemix provides a platform as a service (PaaS) for several different runtimes, specifically:

• Node.js – supporting server-side JavaScript
• Liberty for Java – a highly composable, fast and light profile of IBM WebSphere Application Server designed for the cloud
• Ruby on Rails and Ruby Sinatra – focused on quickly creating web-applications in Ruby with minimal effort

This means that you can deploy an application of any of these types to Bluemix, without having to worry about server setup or configuration.

Let’s go

If you don’t have one already, sign up to get an IBM ID.

Step 1: Sign up for IBM Bluemix

This gets you an account on the brand new IBM Cloud Foundry-based platform as a service (PaaS) cloud. Sign up for the open beta.

It’s well worth exploring the website, the user interface and examples. The rest of this article takes another perspective—the command-line approach!

Step 2: Install the Cloud Foundry CLI, the official command line client for Cloud Foundry.

This is the command that you’ll use to get your code deployed and running on Bluemix.

Install the Cloud Foundry CLI for your system. Scroll down through the readme file to the Installers section for your platform

Step 3: Download the files you’re going to deploy

The kit includes the following parts:

• manifest.yml, which tells Bluemix that it’s being given a node.js application
• server.js, which contains JavaScript code that implements a tiny node.js-based web server
• package.json, which defines to the node.js runtime the dependencies that server.js has

All of these are available as open source on JazzHub, where you will also find:

• readme.md, a very thorough description of server.js, in markdown format

Step 4: A tiny edit; edit manifest.yml to give your host a unique name.

This is important!  Your little server will have a URL that starts with this name.

Step 5: Tell Cloud Foundry to use the Bluemix (web) application programming interface (API)

This command tells the cf command-line that it should direct its interactions at Bluemix. Since this is a web API, all you need to do is specify a URL.

$ cf api https://api.ng.bluemix.net
Setting api endpint to https://api.ng.bluemix.net... OK

You only need to do this once. All subsequent commands will interact with the Bluemix web API.

Step 6: Log in to Bluemix

cf login

Enter the IBM ID and password that you set up.

Step 7: Push your application to Bluemix!

Open up a command line for your operating system and navigate to the place where you saved the files from JazzHub and simply type:

cf push

Lots of magic happens now:

• Using the information in the manifest.yml file, Bluemix knows how to create a runtime for the application
• Using information in package.json, it will realize that this application needs express.js in order to run, and it will run the NodePackage Manager to get that dependency installed (you’ll see lots of information scroll up your screen as express.js and its dependencies get installed)
• Finally, it starts server.js

A few other useful commands to list your apps and to start or stop each one are:

cf apps Lists all of your applications
cf start app_name Starts app_name
cf stop app_name Stops app_name

Step 8. “Bonjour, tout le monde”

Go to your favorite browser and enter the URL:

http://your_server_name.mybluemix.net/english

or

http://your_server_name.mybluemix.net/francais

Alternatively, you can try mine at

http://bonjourlemonde.mybluemix.net/english

Let’s look at each file in turn:

 

manifest.yml

manifest.yml is used by Bluemix to determine what to do with the other files it is given. You can think of it as a way of bundling up a pile of cf command-line options into a file.

applications:
# the URL for the application will be .
# in this example, that's change_this_to_something_unique.ng.bluemix.net
# !! YOU MUST CHANGE host TO SOMETHING UNIQUE !!
- host: change_this_to_something_unique
domain: ng.bluemix.net
# the name of the application that you will see if you type 'cf apps'
name: BonjourLeMondeApp
# the files to 'push' are in the current directory
path: .
# bluemix runs this command to initiate the application
command: node server.js
# tell bluemix to create 1 server instance
instances: 1

package.json

This file is required by the node.js runtime. Apart from naming the package, it defines the dependencies. In this case, the only dependency is on express.js (http://expressjs.com/), a minimal and flexible node.js web application framework

{
"name": "BonjourLeMonde",
"description": "Very simple node.js server using Express",
"version": "0.0.1",
"dependencies": {
"express": "3.x"
}
}

server.js

This file is a very simple node.js application written in JavaScript. Once it’s deployed to Bluemix, if you type
http:///english
into your browser, it responds with “Hello world, from England.” While typing
http:///francais
gets you the response “Bonjour tout le monde, de la France.”

The only point I will note in this article is that these few lines of code (hopefully self evident) are there to provide the response in English.

// specify what to do when http GET is received on http:///english
app.get('/english', function(req,res){
res.send("Hello world, from England.");
}
);

Feel free to experiment by adding your own language support!

If you don’t know node.js

There is a very thorough analysis of every line of server.js in the overview on JazzHub.
It attempts to take you from zero knowledge of node.js to a thorough understanding of server.js.

In this post, I took you thorough a simple example to get some experience with Bluemix. If you would like to continue this discussion, you can connect with me on Twitter at @robphippen.

Add Comment
No Comments

Leave a Reply

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

More Archive Stories

Why we added new map tools to Netcool

I had the opportunity to visit a number of telecommunications clients using IBM Netcool over the last year. We frequently discussed the benefits of have a geographically mapped view of topology. Not just because it was nice “eye candy” in the Network Operations Center (NOC), but because it gives an important geographically-based view of network […]

Continue reading

How to streamline continuous delivery through better auditing

IT managers, does this sound familiar? Just when everything is running smoothly, you encounter the release management process in place for upgrading business applications in the production environment. You get an error notification in one of the workflows running the release management process. It can be especially frustrating when the error is coming from the […]

Continue reading

Want to see the latest from WebSphere Liberty? Join our webcast

We just released the latest release of WebSphere Liberty, 16.0.0.4. It includes many new enhancements to its security, database management and overall performance. Interested in what’s new? Join our webcast on January 11, 2017. Why? Read on. I used to take time to reflect on the year behind me as the calendar year closed out, […]

Continue reading