Tuesday, June 5, 2012

Building HelloSun, Step 2: Hello Node & Heroku

HelloSun: A guide to creating a social networking empire, from scratch, for free, circa 2012.
⇐ Step 1 T.O.C. Step 3 ⇒

In Step 1: Planning, part of the goal was to “use the latest & simplest techniques and tools, and zero upfront costs.”

The “simplest techniques” goal leads me to choose a Platform as a Service (PaaS) solution. With PaaS, the service provider handles most of the infrastructure involved in handling a web server. I simply must write the code that’s unique to my particular application. There is an Ever-Growing List of PaaS Companies to choose from.

The “zero upfront costs” goal narrows down the list to those that offer a free option. Many of them offer free getting-started options, either for a limited time or for a limited amount of CPU and bandwidth.

The “latest techniques and tools” goal makes me want to chose Node.js as the server technology. Node.js is a javascript-based server that, because it almost totally event-driven and non-blocking, leads to efficiency, meaning: this model should allow me to support the most clients possible before having to move to a paid service. I almost went with Ruby, simply as an excuse to learn a new language, but it’s not quite “the latest thing” anymore, I had trouble installing it locally (more on that later), and I hear some disappointing things about its throughput. (I also am persuaded to choose Node.js because of a personal long history behind Javascript, as well as recent fun creating the CrappyNet bad-networking RESTful stress tester.)

I chose Heroku as my PaaS for this project because it supports Node.js and has a nice free option: the equivalent of one server process (enough for Node.js to handle many clients) along with a small free database options (enough for a sunrise-limited window of chats, if that even needs to be part of a database), and because of these additional considerations:
  • Documentation – Heroku’s documentation seems concise and mostly correct.
  • Local Development and Testing – With the Heroku stack I can easily run the app on my local computer very close to how it will run on the server. Thing globally, develop and test locally.
  • Easy Deployment – After testing locally, deploying to Heroku is as easy as a git push.
  • Easy to Switch (no Heroku lock-in) – Because there is nothing special or proprietary to Heroku or the stack I’m using, I can easily stop using Heroku at any time and switch to many other PaaS or, if this service is doing spectacularly well, I can easily move to my own servers at any time.
Get git

git version control is required for working with Heroku. Introducing git is outside the scope of this post, but if you’re doing anything in software these days you need to become familiar with it, so you may as well start at the Official git website.

Install Node locally

Using the Node Installer is super simple. Just download from http://nodejs.org/#download.  I do not recommend getting the latest version of Node from their github repository because those versions are too new to match what is running on Heroku.

After installing node I can run “node --version" at the command line and see that I have “v0.6.18”.  This is close to what is on Heroku and hopefully will work fine.

Get a Heroku Account

Sign Up for an account at https://api.heroku.com/signup.

Install the Heroku Toolbelt Locally

The Heroku documentation is very straightforward at https://devcenter.heroku.com/. In particular, I followed the steps at Getting Started with Node.js on Heroku/Cedar.

In Local Workstation Setup you’ll install the Heroku Toolbelt, which is what will let you run all the necessary Heroku tools from the command line on your local computer.

Write and test your first App

Heroku’s Write Your App instructions worked almost perfectly for me, except that the version of “express” in their example did not match the default I found with the heroku install, and I had a new name for my app. So my version of package.json is:

    "name": "hellosun", // your name, e.g. "hellosunx444"
    "version": "0.0.1",
    "dependencies": {
      "express": "2.5.8"

    "engines": {

      "node": "0.6.x",
      "npm":  "1.1.x"


After following the next set of the instruction at at Getting Started with Node.js on Heroku/Cedar (create web.js, “npm install”, create .gitignore, create Procfile) you should be able to run the code locally with this command:
$ foreman start
Test this with a browser at http://localhost:5000/. If that shows “Hello World!” then you’ve got a local node server running the web.js code. Congratulations!

Deploy your first App to the World Wide Web

Heroku’s Getting Started/Deploy instructions worked fine for me. They are:
$ git init
$ git add .
$ git commit -m "init"
$ heroku create hellosunx444 --stack cedar
$ git push heroku master
$ heroku ps:scale web=1
$ heroku config:add NODE_ENV=production
Testing your first App to the World Wide Web

Browse to http://hellosunx444.herokuapp.com/ and if you now see “Hello World!” then your application is now deployed to heroku. Congratulations!

Changing code and restarting

In step 3 through step X, we’ll be changing this code a lot. So let’s quickly cover how to change the code to “Hello Squirrel!” and redeploy:
  1. Edit web.js to change “Hello World!” to “Hello Squirrel!”
  2. Test locally with “foreman start” and verify that http://localhost:5000/ displays “Hello Squirrel!”
  3. Use git to push your change to web.js to the heroku git server (using your favorite git tools or commands, such as “$ git commit –a”, “$ git push”.
  4. Restart the heroku server with your new code: “$ heroku restart web.1”
  5. Verify that http://hellosunx444.herokuapp.com/ now shows “Hello Squirrel!”
Getting the code

Today’s code is labeled v0001.0001 and is available in these formats:
HelloSun: A guide to creating a social networking empire, from scratch, for free, circa 2012.
⇐ Step 1 T.O.C. Step 3 ⇒

No comments:

Post a Comment