How to Make an Express Web-Server With a Raspberry Pi

Published

Introduction: How to Make an Express Web-Server With a Raspberry Pi

This guide will tell you how to get your Raspberry Pi to host a web-server, which can be used for hosting websites, and even slightly modified to host many other online services such as game servers, or video streaming servers. We will only be covering how to host a basic web-server without editing the default package.

Materials Required:

  • Raspberry Pi (We Recommend the Raspberry Pi 3 Model B+ as that has a built in wireless adapter)
  • Internet access
  • SD Card (Recommended 32GB, Minimum 8GB)
  • USB Keyboard
  • USB Mouse
  • HDMI Cable
  • HDMI capable Monitor
  • Access to your Router's configuration panel (This is for port forwarding)

Step 1: Initialize Your Raspberry Pi

Insert the Micro SD card into another machine, and Install the Raspbian OS onto it using the guide found at https://www.raspberrypi.org if you are starting with a fresh one. Otherwise, ensure your Micro SD card is inserted into your Raspberry Pi.

Step 2: Hook Up Your Peripherals and Log In

Plug in the monitor using the HDMI cable, plug in your USB keyboard, plug in your USB mouse and power source to the Raspberry Pi.

Log into the Pi once it has booted up using the default username and password if requested. The initial username is "pi" and the initial password is "raspberry".

Step 3: Connect the Raspberry Pi to the Internet

If you have access to an Ethernet cable simply plug it in and you are good to go.

If you are using Wifi, there are a couple of different ways to accomplish this. This guide will follow a terminal window approach to ensure, even if you are not utilizing Raspbian's GUI, you can still connect.

  1. Open a Terminal Window if you are utilizing a GUI.
    1. along the bar at the top of the page, the terminal is there.
  2. Ensure your Raspberry Pi detects the Wifi
    1. Enter command `sudo iwlist wlan0 scan`
    2. Look for your access point
      1. ESSID is the name of the access point
      2. IE is the authentication used.
  3. Enter command `wpa_cli` to configure the access point.
  4. Enter `add_network`
    1. This adds a new network to your configuration list.
    2. This returns a single number value in the terminal window. The first network is numbered '0', and this guide assumes that you are setting up the first network. If this is not the case, replace the number that is in the commands with the one this command returned.
  5. Configure the new network
    1. Enter `set_network 0 ssid "ESSID"`
      1. Replace ESSID with the name of the access point. Ensure that the access point's name is in quotations.
      2. Remember, if your add_network command returned a number other than 0, replace the 0 in the command with whatever number that was returned
    2. Enter `set_network 0 psk "PASSWORD"`
      1. Replace PASSWORD with the password of the access point. Ensure that the access point's password is in quotations.
      2. Remember, if your add_network command returned a number other than 0, replace the 0 in the command with whatever number was returned.

  6. Connect to the network by entering the command `select_network 0`
    1. Remember, if your add_network command returned a number other than 0, replace the 0 in the command with whatever number was returned.
  7. Exit the application by typing `quit`

Step 4: Ensure Your Pi Is Up to Date

Within your terminal window, update your system package list by typing `sudo apt-get update -y`.

Next update all installed packages by typing `sudo apt-get dist-upgrade -y`.

Step 5: Install NodeJS Onto the Pi

Within the terminal window, we need to install NodeJS which is a server-side implementation of JavaScript. Express utilizes NodeJS to run. To install NodeJS we need to apt-get it onto the Pi. Enter `sudo apt-get install nodejs -y`. Ensure NodeJS is installed by entering `nodejs -v` to get the current version number installed.

After NodeJS is installed, we need to install the Node Package Manager. This is the package manager that works with NodeJS to install modules, such as ExpressJS. Enter `sudo apt-get install npm -y`. Ensure NPM is installed by typing `npm -v` to get the current version number installed.

Step 6: Install Express and the Express Generator

Once npm in installed, Type "sudo npm install express -g". This downloads ExpressJS in the package manager's global directory, so you can run Express in any folder.

Next, type "sudo npm install express-generator -g". This is a default Express server generator, which is fantastic for quickly setting up a web server.

Step 7: Run the Express Generator

Type `sudo express myapp`. You can replace "myapp" with anything you wish to name your web server.

This creates a folder with the necessary files needed for express to run.

The default view engine will be Pug.

It's highly suggested to look into other options, and what you can do with Express.

Once the Express generator is finished, you can test to see if the web server works. Type `cd myapp` run `npm to enter the folder that was generated, then type `npm start` which will run the application.

Utilizing the default port, open a web browser and navigate to localhost:3000. You should see 'Express, Welcome to Express'. You have successfully created an Express web server.

Step 8: Set the Server Up to Run on Boot-up

Edit the rc.local file. This file runs every time the Pi boots up

Do this by typing "sudo nano /etc/rc.local"

Before the line "exit 0" add the following line: "su pi -c ‘node /home/pi/myapp/server.js < /dev/null $’"

Replace the 'myapp' portion of the folder structure with whatever you named your web server to be.

Save the file by hitting CTRL + x.

Step 9: (OPTIONAL) Portforwarding

This is a key step if you want your web server to be view able from outside your home or school network. This process will be different depending on the router, but the idea stays the same. You can find a specific guide for your router here https://portforward.com/router.htm using your routers model number. The picture above shows an example of what the web-page may look like, taken from portforward.com

  1. Gain access to your routers control panel
  2. Gather 2 pieces of information, your the raspberry pi's internal IP address, and the external IP address of the router
    1. The internal IP address of the pi can be found by typing "sudo ip addr show" in the terminal
    2. The external IP address can be found by typing in "What's my IP address" on google.
  3. Inside of your routers configuration panel you want to set up a rule using the internal IP address of the raspberry pi
  4. choose TCP and for the port you can use whatever port you set on your node server in the express app, we used 1337

Step 10: Access the Webserver

You can now access your web-server from a remote location, to try it out just type http://INTERNAL_IP_ADDRESS:1337, making sure to replace "INTERNAL_IP_ADDRESS" with the external IP if you are accessing the web-server from an outside network, or using the Internal IP if you are accessing it from within the same network.

Share

    Recommendations

    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    Discussions

    Very helpful information about the raspberry pi!