In this article, you will learn how to build a simple Node.js application and deploy it to IBM's BlueMix platform. BlueMix makes it easy to deploy your apps into production, and provides a variety of services that simplify common tasks such as data storage, caching, mobile services and more.
This article covers:
- Building a Node.js app locally
- Creating a BlueMix application
- Deploying the app
Building a Node.js app locally
We're going to build a really basic Hello, World type app. You'll need to have Node.js installed on your machine - so if you haven't done so already, head over to http://nodejs.org/ and follow the guidelines there to get set up. If you are a Mac user and have Homebrew installed you can install it using the following command:
brew install node
Once you have Node installed, you can check that it's working using the following command:
You should see a response such as (the version number isn't important, the main thing is that you don't get an error):
To build the app, we'll use the Express.js framework and a command-line generator that makes it simple to create a new Express-based project.
npm install express -g npm install express-generator -g
If you are running on Mac or Linux you will probably need to prefix the above commands with
Next, let's generate an Express app.
This will create a new directory named
bmtestapp. Next, we'll change to that directory and install the npm dependencies needed for Express to work.
cd bmtestapp npm install
Finally, we'll run the app locally:
You should see the following output:
> firstname.lastname@example.org start /Users/jlennon/Projects/bmtestapp > node ./bin/www
You can now run your app by navigating to http://localhost:3000 in your Web browser. You should see a screen that looks like the screenshot below.
Creating a BlueMix application
Now that we have the app running locally, let's get it deployed to BlueMix. You'll need to go to the BlueMix website and sign up for the Beta to create a BlueMix application.
BlueMix is built on the CloudFoundry platform, and to work with it from the command line you can use the CloudFoundry CLI tools - download the relevant installer for your platform. Again, if you're using a Mac, you can easily install it with Homebrew:
brew install cloudfoundry-cli
When the tools are installed, you can check that they are working correctly using the following command:
You should see a response like (again, the version isn't too important here):
cf version 6.1.0-homebrew
Next, tell the
cf tool where the BlueMix API can be found (you'll only need to do this the first time you use
cf api https://api.ng.bluemix.net
Now, login to BlueMix using the credentials you used to sign up for the Beta:
Your username is your email address.
Deploying the app
Now that you have the
cf tool configured and you have logged into BlueMix, deploying your app is really easy.
cf push jl-bmtestapp
jl-bmtestappin the command above is a unique name for your app on BlueMix - make sure you change it to something else. This will form part of the URL for your app.
This will create a new application on BlueMix, upload your code and download any dependencies, and start your app. You should see the URL for your app in the output - the URL for my app is http://jl-bmtestapp.ng.bluemix.net. Copy the URL for your own app and run it in your Web browser. You should see your Express app again, but this time it's running on the cloud via BlueMix.
Anytime you make changes to your app, simply run the above command again to update and re-deploy your app.
In future articles I will talk about how to use BlueMix services in your apps and integrate with a Git repository so you can auto-deploy your app every time you push changes to Git.
Joe is CEO of Subwoofr, a platform that helps music artists make more money. He is the author of HTML5 in Action and Beginning CouchDB, and is a regular contributor to IBM developerWorks.