The Raspberry Pi 2 is a wonderful little piece of hardware. Why not use it for a...

"Node.js"

development server, or maybe a GPIO project, all built using JavaScript.

This post assumes you are using a Raspberry Pi 2 already setup and are ready to walk through the steps of installing and configuring node.js and PM2 on your shiny little Raspberry Pi 2. You will create a basic node.js application, set it to automatically load on boot and restart your application whenever changes are made. Let's get started!

Setup the Safe Account

It is a good idea to use a "safe" account for running your node.js app that isn't the default pi admin account.

Add the new user:

sudo useradd -s /bin/bash -m -d /home/myuser -c "safe myuser account" myuser

Set the password (you will be prompted twice to enter new password):

sudo passwd myuser

Give safe user account access to root-level commands:

sudo usermod -aG sudo myuser

Login with the new safe user account:

ssh myuser@192.168.xxx.xxx (replace with ip address of your Pi)

Installing and Configuring node.js

Download and install the latest ARM-version of node.js built for use on the Raspberry Pi(2):

wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb

Verify the installation and return a version #:

node -v

Give the safe user account permission to use default HTTP port 80 by installing the libcap2-bin package and applying permissions:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node

Create the Basic node.js App

We'll use the nano editor to create our basic node.js application. Start by opening nano to create the app.js file:

nano app.js

Add the following JavaScript syntax to create the basic app, establish the server and output a console message:

var http = require('http');
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello from nodejs running on Rasbian!\n");
});
server.listen(80);
console.log("Server running at http://127.0.0.1:80/");

Press CTL-X to exit and Y to save the app.js file.

Launch the node app by opening a terminal and executing:

node app.js

View the running node app by opening a browser and navigating to http://localhost or http://127.0.0.1. The node JavaScript syntax above configures the server to use the default HTTP port of 80 so there is no need to include a port number when viewing the app in the browser. To stop the server, press CTL-C in terminal window.

Installing and Configuring PM2

node.js, got it. What exactly is PM2?

NPM

PM2 is a production process manager for node.js/io.js applications with a built-in load balancer. It allows you to keep applications pretty much alive forever, to reload them without downtime and to facilitate common system admin tasks. For the purpose of this post, we'll simply be using PM2 to "watch" the basic node app for changes, restart the app if any are made and auto boot the node.js server on device power on.

Install the PM2 NPM Package globally:

sudo npm install -g pm2

Configure PM2 to run as a service under the safe account created earlier:

sudo env PATH=$PATH:/usr/local/bin pm2 startup -u myuser
pm2 save

This will create a script called pm2-init.sh in the /etc/init.d directory. It is important to understand how this works. This script does NOT start the app.js file created earlier. Instead, it starts the apps that were running under PM2 the last time the server shutdown.

As of 05/28/2015, there is a small issue with the pm2-init.sh script referencing a root home directory path that will cause the service daemon to not start on reboot. To correct this and use the safe account (myuser in this example) that the service is registered under, you must edit the /etc/init.d/pm2-init.sh script and replace the /root with /home/myuser (or whatever account you've configured) to fix the auto loading of PM2 applications on reboot.

sudo nano /etc/init.d/pm2-init.sh

to open the script and change

export PM2_HOME="/root/.pm2"

to

export PM2_HOME="/home/myuser/.pm2"

Press CTL-X to exit and Y to save the pm2-init.sh file.

Launching the node App w/ PM2

Open up a terminal and execute the following to launch the node app with PM2 and add it to PM2's list of applications:

pm2 start app.js --watch

You should now be able to open a browser, point it to your Raspberry Pi's ip address and see the Hello from nodejs running on Rasbian! text output from the sample node application you created earlier.

Advantages of starting your node app this way are:

  • Using the --watch flag will restart your application if changes are detected
  • PM2 will automatically restart your application if it crashes
  • PM2 will keep a log of your un-handled exceptions - in this case, in a file at /home/myuser/.pm2/logs/app-err.log
  • With this one command, PM2 will ensure that any applications it manages restart automatically when the server is rebooted (i.e.: your node application as a service)

In Closing...

You've now now have a self-booting, node.js server that fits in the palm of your hand. And that's only the beginning of what you can do with this awesome device. The inspiration for this post comes from a developing Pinewood Derby timing system used to monitor track results and display them to participants with node.js via projector and HDMI port.

I'm curious to see what you do with your Raspberry Pi 2. Have fun!