# Running an express web server on Raspberry Pi

#2Articles1Week

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

%[https://blog.saiyerniakhil.in/setting-up-raspberry-pi-to-run-a-web-server] 

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](https://docs.microsoft.com/en-us/windows/wsl/install-win10). Run the below command the proceed by entering the password as prompted.

```plaintext
 ssh pi@raspberrypi
```

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

```plaintext
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.

```plaintext
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.

```plaintext
vim index.js
```

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

```plaintext
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 -

```plaintext
node index.js
```

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

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619885371386/wubtpIWFZ.png align="left")

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

Thumbnail/Cover Photo by [John McArthur](https://unsplash.com/@snowjam?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/express?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)
