James Wood Creative

Raspberry Pi Development Server

1 January, 2015

Replicating a Camel/Node.js setup from Heroku onto my Raspberry Pi was really simple. Far simpler than I expected!

In the previous post I talked about setting up a website using Heroku, Node and Camel. This is working really well so far, but every change needs to be pushed live using Git. Which has a few great points, but one bad one: when editing or playing with the site, I spend more time pushing things live than I do actually coding or writing. For this reason, I decided to build a local copy of the site that I can use for development, then sync it back up to the Heroku instance when I’m happy.

I decided to set this up on a Raspberry Pi, because I’m interested to see if the Pi itself can be made stable enough to eventually become the main server for this very low traffic site.

After installing Node.js and NPM on the Raspberry Pi, all that was left was to install Heroku’s Linux Toolbelt.

The Toolbelt is connected to your Heroku account by running heroku login from the terminal. Then I made a new folder for Heroku stuff, with mkdir Heroku. I switched to that folder with cd Heroku. Then the existing Heroku Git repository is cloned with heroku git:clone -a j-w (where “j-w” is your existing app’s name, not mine…). This copies all the files for the website onto the local machine, into a subfolder named after the Heroku instance (in this case, j-w). So for me, the files sit in ~/Heroku/j-w/.

Node uses a system of Modules to provide additional functionality. These are normally installed manually, but Heroku automatically includes all the necessary Node modules for you when you list them in a package.json file, like this:

"dependencies": {  
    "compression": "^1.2.1",  
    "express": "^4.10.4",  
    "handlebars": "^2.0.0",  
    "marked": "^0.3.2",  
    "q-io": "^1.11.6",  
    "rss": "^0.3.2",  
    "sugar": "^1.4.1",  
    "underscore": "^1.6.0"  

For the Raspberry Pi site, the packages need to be installed locally. This is done by switching to the website folder and running npm install. This checks the above package.json file and installs all the needed modules into a subfolder named node_modules/, within the website folder.

These packages should not be pushed back up to Heroku. To stop this, I made a file called .gitignore in the website folder, which contains the line node_modules/ - this simply tells Git to ignore the Node modules folder. Easy!

Running the site locally with node camel.js now serves a test version of the site on the raspberry pi, which I can then push back up to Heroku when it’s ready (ish) to go live. This is done with a few commands.

Firstly, new files (that aren’t in /node_modules/) are added to the repository with this command:

git add .

Then any new or changed files are committed to the local repository with

git commit -am "Comment here so I remember what I changed"

Finally, the new or changed files are pushed to Heroku with

git push heroku master