Hosting Node.js Apps on Bluemix
Join the DZone community and get the full member experience.Join For Free
one of the biggest issues i had when learning node.js was how to take an application into production. luckily theres multiple options to make this easier, including ibm bluemix , a service i’ve been playing with over the past few weeks. in this post, i’m going to briefly describe what it takes to set up a new node.js app on bluemix as well as what it is like to migrate an existing site there.
ok, so first off, you’ll need to sign up for bluemix. you won’t need a credit card and you get a good long trial to play with things. there’s a lot of cool stuff at bluemix, far beyond what i’m talking about today, so the trial will give you time to play around with other features as well.
the very first time you sign in, you’re asked to create a “space”. you can think of this as a bucket for your various apps and services. you can name this whatever you want, and you’ll just need to do this one time.
after you’ve done that, you can then create an app.
next, select web.
and then select sdk for node.js
for app name, select whatever makes sense.
note that the app has to be unique across all of bluemix. so you may need to prefix the name of your app with something unique. so for example, if you were launching raymondcamden.com there, i’d pick a name that included that. (and yes, the name i used in the screen shot above was taken. i switched to rkcmyapp.
on the next screen, you’re given some good information to help you get started.
pay particular attention to the “cf command line interface” download. this is the command line tool that you will use to help push updates from your machine to bluemix. this is a one time install. unfortunately you can’t use npm to do the install, but hopefully in the future those tools will be published there.
you can, if you want, also download the starter app code. that’s the code currently being used for your new application. if you are new to node and want to learn, i’d recommend grabbing the code and playing with it. it uses express (which in my opinion is one of the best node libraries out there) but also uses jade, which is the template framework of the devil. luckily you can easily switch to a more sensible framework quickly enough.
go ahead and dismiss that welcome text to view your application console. if you ever want to get back to it, it is available by clicking “start coding” in the left hand nav.
you can see options for adding new instances, increasing memory, and adding new services. you can stop and restart the app as well as seeing a log of recent changes. finally note the “routes” section on top. clicking on the url there will take you to your application.
updating and deploying your code is pretty easy. assuming you’ve got node already running locally, and assuming you grabbed the “starter pack” for this new app, open up the files in your editor and at your terminal, get it running by first installing dependancies (npm install) and then running it (node app). by the way, i strongly recommend nodemon while developing.
after you’ve made your changes, how do you push it up to bluemix?
first, you have to tell the command line (cf) what api to use. this is a one time setting.
cf api https://api.ng.bluemix.net
cf login -u youremail -o yourorg -s yourspace
this login will persist (i’m not sure how long), so you won’t have to constantly relogin as you do stuff.
pushing updates to your app then is trivial:
cf push appname
the update will take about a minute. i’m guessing if you add a bunch of new libraries to your package.json file it may take longer. but once done, you can hit your site and see it changed. you can see my app here, but note that i created this in a trial account that is ending in three days: http://rkcmyapp.mybluemix.net/ . in case my trial account has expired, here is what i created in about 20 seconds:
the command line has a heck of a lot of power (basically everything the site has). you can run
by itself to see docs at the command line. one option i found recently was
cf logs myapp
. this will begin tailing your logs, from your server, right in your terminal.
migrating to bluemix
so all of the previous is related to working on a new node.js app with bluemix. what about migrating an existing app?
first, you want to pay particular attention to the node.js bluemix documentation on deployment . bluemix runs a customized version of node.js. it isn’t directly linked to from there, but you can find details about the modifications here: ibm sdk for node.js version 1.1 .
in the docs linked to above, pay special attention to this portion:
if procfile is not present, the ibm node.js buildpack checks for a scripts.start entry in the package.json file. if a start script entry is present, a procfile is generated automatically. otherwise, ibm node.js buildpack checks for a server.js file in the root directory of your application. if a server.js file is found, a procfile is also generated automatically.
setting up your domain to point to your bluemix server is also pretty easy, and i was going to write out the process with screen shots, but then i discovered this good video by ryan baxter and i figured it didn’t make sense to duplicate the effort.
so, what do you think? have you tried bluemix yet? if so, did it work ok for you? let me know in the comments below!
Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
10 Traits That Separate the Best Devs From the Crowd
Best Practices for Securing Infrastructure as Code (Iac) In the DevOps SDLC
Security Challenges for Microservice Applications in Multi-Cloud Environments
Azure Virtual Machines