Setting Up A Node.js Server On Your Raspberry Pi
Setting Up A Node.js Server On Your Raspberry Pi

In this Tutorial I will explain you about setting up a Node.js server on your Raspberry Pi. In this setup the server will automatically start on device start-up.

To get technical information about Raspberry Pi 2 visit this link. For Raspberry Pi 3 visit this link. Also before starting take a look at Node.js official site.

For this setup you need:

  • Raspberry Pi (2 and 3 are recommended)
  • A Computer (Optional)
  • Working Internet Connection

First of all we are going to set up our board(i.e. Raspberry Pi). For this tutorial I’m using Raspberry Pi 2 and Linux (Ubuntu). All the following steps will probably run smoothly on Raspberry Pi 3.

Also Read:
5 Operating Systems That You Can Run With Raspberry Pi
Take Screenshot On The Raspberry Pi At a Specified Time

Step 1: Install Raspbian On a SD Card

If you’re using Windows follow these steps. For Linux these steps. For Mac OS these steps.

Step 2: Booting Up The Board

After installing Raspbian on a SD Card. Put the SD Card in its socket and connect a regular 5V power supply to the board. For power you can use a USB port in a computer or a Power Adapter connected to a wall socket. Using a Power Adapter is recommended. Now connect the ethernet cable to the board or connect using WiFi if you are using Raspberry Pi 3.

Step 3: Connecting To The Board

In order to perform installation of Node.js and other softwares we need access to the operating system. For access we can directly connect the board to a monitor or establish a SSH connection. In this tutorial we are going to access through SSH. If you are willing to access using the monitor its fine. The steps are same after the we establish the SSH connection.

Step 4: Establish SSH Connection

If you are using Windows download putty from here. It’s a SSH client software for Windows. If you are using Linux or Mac you don’t have to worry about any Software. Now we need the IP address of the board. To do that we can access our network router’s setting to see what IP did the DHCP assign to the board or we can connect a monitor once and open the terminal and run the command

pi@raspberrypi:~ $ ifconfig

This will display the network information with the IP address.

ifconfig - Node.js Server on Raspberry Pi

We can see that IP address is the ‘inet addr’ field with 192.168.1.35. Now use the following command to establish SSH connection

$ ssh [email protected]

If you are connecting for the first time it will display some message about authenticity, just type “yes” and hit enter.

Now you need to enter the password. The default password is ‘raspberry’. Enter the default password and hit enter.

If you are using putty just use the IP address and connect. Use ‘pi’ as username and ‘raspberry’ as password.

Step 5: Install Node.js

Node.js can be installed in two ways. Through apt or directly downloading the package. We are going to directly download the package. There are two versions of Node.js. LTS and Current. LTS stands for Long Term support which means that the package version will be supported in future and obviously has more people using it and current version is constantly updated with major changes and may contain bugs and potential security exploits. So we are going to use the LTS version. The current LTS version on the date of writing this tutorial is 4.4.7.

We can download the LTS version directly from the official site or we can use the following command

pi@raspberrypi:~ $ wget https://nodejs.org/dist/v4.4.7/node-v4.4.7-linux-x86.tar.xz

This will download the file in your Home folder. Now we need to extract the package. Use the following command

pi@raspberrypi:~ $ tar -xf node-v4.4.7-linux-x86.tar.xz

This will create a folder named ‘node-v4.4.7-linux-x86’ and extract all the files of package into it.

Now we need to add the path of our Node.js folder into PATH variable. To do that we need to edit ~/.profile file.

pi@raspberrypi:~ $ nano .profile

Now find the following line

 PATH="$HOME/bin:$PATH"

Now change it to

PATH="$HOME/bin:/home/pi/node-v4.4.7-linux-x86/bin/:$PATH"

Now save the file with Ctrl+o and hit Enter. Exit with Ctrl+x.

Step 6: Create your server script files

I’m not going to explain this step because now you need to create your server in Node.js and save it somewhere. For example lets assume that the server file named ‘server.js’ is saved at Desktop.

Step 7: Run the server

Now use the following command to run the server

pi@raspberrypi:~ $ node /home/pi/Desktop/server.js

Now go the browser and check your server.

Step 8: Keeping it running ‘forever’

If you close the terminal or interrupt the execution of command the server will stop. But we want the server to keep running even when the terminal is closed. To do that we are going to need the help of another package called ‘forever’. We will install this package using the npm (Node Package Manager). To install use the following command

pi@raspberrypi:~ $ npm install forever -g

After installing forever. Navigate to Desktop and run the following command to run server script forever.

pi@raspberrypi:~ $ forever start server.js

This will keep your script running even after you close the terminal. But to start the server script on device start up follow the next step.

Step 9: Run The Server At Start Up

To run the server script at start up is easy. Edit ~/.profile with the following command

pi@raspberrypi:~ $ nano ./profile

Now add the following command at bottom of the file

forever start /home/pi/Desktop/server.js

Now save the file with Ctrl+o and hit Enter. Exit with Ctrl+x.

Step 10: Reboot The Device

Use the following command to reboot the Device

pi@raspberrypi:~ $ sudo reboot

Your Server should now be running on start up. You can change the contents of server.js or add another file in ~/.profile in the same way we did before.

Any suggestion, correction and comment are welcome.

By Gadgetsay Newsroom

Gadgetsay is all about the latest and viral with interesting valuable content, the content which related to smartphones, gadgets and other tech news. Follow us on twitter and facebook to get the latest news.