In this tutorial I will explain how to install a NodeJS Web Server on a Raspberry Pi running Raspbian for the OS. I am going to use Windows 7 as the developer machine but the same process will also work on Windows 10. I will also show you how to run the server on startup.

Setting up the Raspberry Pi

I am not going to go into details on how to get Raspbian installed and how you shell into a terminal on a linux box as that is covered on many other posts. To run a NodeJS Web server the first thing to do is install nodejs on the Raspberry Pi, in a terminal window run the following commands.

First we will update the Raspbian installation by running the following command.

sudo apt-get -y update

From what I’ve read, Rasbian comes with node out of the box, but we’ll want to make sure we have the most recent build. To install the latest version of Node, make sure you’re online and run this command:

sudo su;
wget -O - https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh | bash;
exit;

That's it! Node is nou installed. You can check by typing the following:

node -v

You should now see something similar to this:

The React App

Create a folder to store all our file in with this command:

mkdir nodeServerA

Now run this command to get into the new folder

cd nodeServerA

To start, I’m using an incredibly simple app that includes express and httpster so serve files. You can get these installed and saved with the following command:

sudo npm install -g express httpster 

You should now see this:

The goal is to start off with something very simple. You probably already know how to build a js app, and the last thing you want are any headaches from your application.

To begin we’ll make server.js look something like this:

var http = require("http");
var server = http.createServer(function(req, res){
  res.writeHead(200,{"Content-Type":"text/plain"});
  res.end("Hello World");
});
server.listen(2021);
   
console.log("Server is listenning on port 2021");

Now we can build and launch our app with:

node server.js 

Open up a web browser and navigate to your Raspberry Pi on port 2021. You should see this:

Runnig the Server on Boot

A working web app on a Raspberry Pi is fun and all, but it’s no use unless we can have the server running all the time. The tricky part is getting your RPi to spin up the server whenever it boots up. In order to do this we’ll need to add a script to the rc.local file in /etc/, which runs everytime your RPi is starting up.
First let’s open our file:

sudo nano /etc/rc.local 

On the line before ‘exit 0’ write the following script:

 su pi -c 'node /home/pi/nodeServerA/server.js < /dev/null &' 

Write out the lines in order to save them (CTRL-X) and then restart your Pi with:

sudo reboot 

You can list running processes with this command:

ps x

If you look at the PID column you will notice that our server has an id of 441

To stop the process from running just do this:

kill 441

What’s Next?

With this set up, you can essentially create a real-world development server for testing your apps.

This is new for me too, so I’m going to be experimenting and tweaking the process to make it better

More to come, cheers!