Sai Yerni Akhil's Blog

Sai Yerni Akhil's Blog

Running an express web server on Raspberry Pi

Running an express web server on Raspberry Pi


This will be the second post of the raspberry pi series. You can check out the first one from here -

If you remember from the previous article, we set up our web server so that it can be accessed without having to connect the Pi to a computer. Now is the time to get down to business, which is setting up and running a webserver. You should use whatever stack you're most comfortable with, but in this case, I'm using a Node/Express.js API.

The PuTTY utility can be used to ssh into the Pi, but I'm using the ssh utility from the Linux running on WSL. Run the below command the proceed by entering the password as prompted.

 ssh pi@raspberrypi

Run the commands below to update the Linux packages and also get the things we need

sudo apt update
sudo apt-get install vim
sudo apt-get install node.js
sudo apt-get install npm

Once that's done, we're ready to go, and we'll need to create a npm project to enable us to run a webserver on the Pi.

Setting up an express server.

mkdir code-hello-world && cd code-hello-world
npm init -y

After the project is set up, install the express package and create an index.js file.

vim index.js

In that index.js, put in the following code, which is the JavaScript code to serve hello-world from the Pi.

const express = require('express')
const app = express()
const PORT = 3000

app.get("/", (req, res) => {
        res.status(200).send("<h1> Hello, World! </h1>")

app.listen(PORT, (req, res) => {
        console.log(`R_PI listening from port ${PORT}`)

Now everything's set up, the only thing that's remaining is to run the express.js server -

node index.js

You'll now have the server running on the Pi.


Please do let me know how you felt about this post.

Thumbnail/Cover Photo by John McArthur on Unsplash

Share this