Hosting a Node.js HTTP Server on a Raspberry Pi

 

The Raspberry Pi is a great tool for running your own mini server from home. If you have multiple Pis you are also able to experiment with distribution of work, and balance loaders. However, as impressive as those features may be, you will first need a running server on your Pi. I will try my best to walk you through the process. Just follow these simple steps:

Set up your Raspberry Pi

To get started you will need the Raspbian operating system installed on your Pi. The process of burning the OS image onto your SD card is thoroughly explained in their Getting Started Guide. You will also need the Pi connected to your router via an ethernet cable or wireless dongle.

Open up some ports
Our web server will be hosted on port 8080 and we must therefore open traffic to that port. Anyone accessing our external IP address under that port will be forwarded to our device's IP address. The important ports to open are:

  • Port 22 for SSH - with extra precautions
  • Port 21 for FTP - with extra precautions
  • Port 8080 for our web server traffic. You can use any port over > 1024.

If you don't want your website to require a port in the address simply pre-route all traffic from 8080 to port 80 with the following command:

$ sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 8080

This needs to be re-run each time you reboot your Pi. Add the line to the '/etc/rc.local' file.

Wirelessly connect to your Pi (Optional)

If you wish to wirelessly manage your Pi you will need to establish a secure shell(SSH) connection with it. To do this you will need the Pi's local IP address. This can be found in your router's connected devices page, or through the following terminal command:

$ ifconfig

Once that is done, connect via your PC on the same network with an SSH client. Use PuTTY if you're on Windows or this terminal command if on Linux/Mac:

$ ssh piUsername@<local ip address, for example: 192.168.1.5>

Both of these methods will require your Pi's password. If they don't then you need to set one right now! Without password protection, anybody has the ability to connect to your server! Read How to Restrict SSH Access Only to Specific IPs for an extra layer of security.

Installing Node.js

You can now install Node with the following terminal commands:

$ curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
$ sudo apt-get install -y nodejs

Upload a simple web application to your Pi

You can upload any web application you may have to your Raspberry Pi. An easy way to do this is via FTP or SSHFS which will allow for easy drag and dropping. Here is a simple Express app I uploaded for demo purposes(Remember to ‘npm install express’):

const express = require('express');
var app = express();
const port = 8080;

app.get('/', (req, res) => {
  res.send('Hello World!')
});

app.listen(port, () => {
  console.log(`Your Raspberry Pi HTTP server is listening at http://localhost:${port}`);
});

Run it with node

Start up your web application with node like so:

$ node app.js

Once started you may visit the application on your local network at http://<local_ip>:<port>/ . To visit it from outside of your network you will need your public IP address instead. Getting your public IP address is as easy as visiting IPChicken.

Important note: You cannot visit the site via your external domain if you are on the same network. If on the same network, use the local address. If you want to test that the public IP is correctly routing to your Pi server use your cellphone data or another WiFi connection. Another important note is that your ISP is likely not giving you a static IP address. This means that your site will change IP's periodically. Look into dynamic DNS or getting a static IP.

Next steps

You have a very basic Node.js HTTP server running on your Pi. You may now look to expand on that with some extra features such as the following:

  • Domains/Subdomains: DOT.tk offers free .tk domains that are great for development!
  • DDos protection: Look into providers to ensure that your bandwidth won't be abused.
  • MongoDB/MySQL: Why not host a database on your Pi?

That's all folks! If you have any questions, feel free to ask them in the comments section below and I'll get back to you as soon as I can. Remember, this tutorial is just for fun. You won't scale to tens of thousands of daily visitors on your Raspberry Pi. If your power goes out so does your website. Enjoy!