Verify your IBM Bluemix account
Make sure that you have a working IBM ID before you start trying out the application and the instructions in this post. To obtain an IBM ID:
- Visit https://www.ibm.com/account/profile/us?page=reg
- Complete the 2 step registration form
- In case you have questions check out the IBM ID registration FAQ here: https://www.ibm.com/account/profile/us?page=regfaqhelp
To verify that your IBM ID is working, visit IBM Bluemix https://ace.ng.bluemix.net/ and sign in with the IBM ID by using the Login button on the upper right. Note that login to IBM Bluemix may break when using Google Chrome with incognito browsing. While writing this blog post, I found that using Chrome in this mode redirected me to https://idaas.ng.bluemix.net/idaas/public/nullnull
and generated an HTML page with the following error message
Error 404: java.io.FileNotFoundException: SRVE0190E: File not found: /public/nullnull
If you encounter a similar problem you should complete the rest of these instructions with incognito browsing turned off.
Prepare the application source code
If you don't have git installed or don't want to use git, you can manually create a separate directory and then copy and paste the source code for main.js and package.json files described here. Otherwise, the git repository for this post is available via https://github.com/cosipov/minimal-hello-world-node-js
var port = (process.env.VCAP_APP_PORT || 8192);
relies on process.env.VCAP_APP_PORT variable which is automatically initialized by the Bluemix environment to 80, a default port for HTTP traffic. Use of the || operator (logical or) on the same line ensures that if we run the main.js code on a local Node.js runtime (i.e. outside of the Bluemix environment), the port number will default to 8192 since VCAP_APP_PORT would be null. Line 2 uses similar logic to set the hostname of the application.
The following JSON code for package.json file is needed to describe to BlueMix that the application requires a Node.js runtime. Regardless of whether you have cloned the git repository for the application or copied and pasted the source code from the snippets above, at this point you should have a directory containing main.js and package.json files.
Deploy and run the application on Bluemix
Both Bluemix and Cloud Foundry rely on a Go based command line tool (called cf) to script common application management tasks. The getting started section of the Bluemix documentation provides references on how to install cf the Cloud Foundry command line tool. Once cf is installed, you can use it to connect to IBM Bluemix as the deployment platform
and then login to Bluemix using your IBM ID (in line 1)
If the target and login commands completed successful you should be ready to deploy and run the application on Bluemix. Ensure that your working directory contains the application files main.js and package.json before running the cf push operation shown in the following snippet. Note that the -n argument specifies that hello-world-node-js-0123456789 should be used as the subdomain(hostname) for the application, -m tells Bluemix to allocate 128 megabytes of memory to the application, while the -c argument tells the Node.js runtime in Bluemix to run the code in the main.js file.
Once started, the cf tool will show you information about the progress of the deployment. If the deployment processes finishes correctly, you should see the "App started" message and your browser should show the Hello World message when visiting https://hello-world-node-js-0123456789.ng.bluemix.net An example of a push session is shown below. Note that parts with your <Your IBM ID> will be different for your session.
Since multiple Bluemix users may try to deploy an application to the same subdomain you may see the 'host is taken' error message (example below) during the deployment process. To work around this problem, simply change the subdomain to a different, unique value.
Simplify deployment with manifest.yml
A better alternative to having the deployment parameters in the command line is to create a Cloud Foundry manifest.yml file. As shown in following snippet the manifest file contains the values that can be specified during the push process. By convention the file starts with three dashes followed by YAML based markup. A detailed description of the manifest file specification is available from the Cloud Foundry website. The next time you will execute cf push, the cf tool will automatically use the contents of the manifest to deploy the application to Bluemix.