How to Set Up a Node.js Web Server on Raspberry Pi

How to Set Up a Node.js Web Server on Raspberry Pi

A couple of years ago, I bought a Raspberry Pi Model B+, and I finally decided to set up a web server on it.

Raspberry Pi Model B+
Raspberry Pi Model B+

This might sound obvious, but I now understand that a Raspberry Pi is essentially a mini PC, which means it requires an operating system (OS) to function. This is different from other boards, like Arduino, where you can just run your program without needing to install an OS.

Here are the steps to set up a Node.js web server on a Raspberry Pi from scratch:

1. Install Raspberry Pi Imager

Imager will help you install Raspberry Pi OS onto a microSD card.

While there are other OS options like Debian, Ubuntu, etc., Raspberry Pi recommends using the official Raspberry Pi OS for the best experience with their boards.

2. Install Raspberry Pi OS

Open Raspberry Pi Imager and select:

  • Raspberry Pi Device: Select your model.
  • Operating System: Choose the recommended option.
  • Storage: Select your microSD card.


Raspberry Pi OS Setup with Raspberry Pi Imager
Raspberry Pi OS Setup with Raspberry Pi Imager

I went with the default settings, and once it finished, it showed this message:


Raspberry Pi Imager Setup Success Message
Raspberry Pi Imager Setup Success Message

3. Insert the microSD card: Place it into your Raspberry Pi board.

Along with the microSD card, connect any other peripherals, such as:

  • Mouse
  • Keyboard
  • Monitor
  • Ethernet cable
  • Power cable (it's recommended to plug this in last).


Raspberry Pi Peripherals
Raspberry Pi Peripherals

It's recommended to plug in the power cable last. Notice that the microSD card is inserted on the opposite side of the board.


Raspberry Pi Board MicroSD Card
Raspberry Pi Board MicroSD Card

Once Raspberry Pi OS boots up, you'll see the desktop welcome message, which should look something like this:


Raspberry Pi OS Desktop Environment
Raspberry Pi OS Desktop Environment

Finally, you'll see the desktop environment, which looks like this:


Raspberry Pi OS Desktop Environment
Raspberry Pi OS Desktop Environment

This means your Raspberry Pi OS is ready to use.

Install Updates

Once your board is turned on, it will take some time (in my case, about 2 minutes) to boot up and display the desktop UI. Once it's ready, open the terminal and run the following commands:

Update your system packages

sudo apt-get update -y        

Update installed packages

sudo apt-get dist-upgrade -y        

Install Nodejs

sudo apt-get install nodejs -y        

Let's install npm as well

sudo apt-get install npm -y        
$ node -v
v18.19.0

$ npm -v
9.2.0        

Install Express

Express is an npm package that lets you easily run a web server. I used their generator and stuck with the default options.

npx express-generator        

  • Say yes to the default settings

Install npm packages

npm install        

Run the Server

npm start        

By default, the web server runs on PORT 3000. To access it from another machine, you just need the Raspberry Pi's IP address. For instance, my Raspberry Pi's IP is 192.168.100.239, but yours will likely be different.

Open the Web Server on Another Machine

Go to your regular machine, open the browser, and paste the IP and PORT (e.g., https://192.168.1.239:3000) in the address bar. You should see something like this:

https://192.168.100.239:3000/        


Raspberry Pi Web Server Accessed From Another Machine
Raspberry Pi Web Server Accessed From Another Machine

If everything is set up correctly, your Express app will now be accessible from any device on the same network.

In my case, since I'm using the Raspberry Pi B+ board, the RAM and CPU aren't as powerful, so it took a bit of time to run each command. It’s normal for lower-spec devices like this to take longer for tasks like installing dependencies or starting the server.

Conclusion

The Raspberry Pi is essentially a mini PC, offering seamless integration with IoT devices. You can connect sensors or just about any electronic gadget to the board.

While the Model B+ isn't the most powerful option, there are more advanced boards available now, and it's safe to assume the Raspberry Pi team will keep enhancing their devices in the future.

Extra

Initially, I wanted to run Next.js on the Raspberry Pi, but I encountered the following error:

<--- JS stacktrace --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
Aborted        

So, I ultimately decided to switch to Express. Remember, this is an older board with limited resources. Modern boards should have more power. The cool part is that, since it runs a Linux OS, you can install pretty much any package.

要查看或添加评论,请登录

Jaime Garcia Diaz的更多文章

  • React 19: New API use(promise)

    React 19: New API use(promise)

    In this post, I’ll demo how to read a value from a promise using use. Links Demo Codebase Snippet Let’s check out the…

    1 条评论
  • Web Development: Three Predictions for 2025

    Web Development: Three Predictions for 2025

    In my last post, I shared three things I learned during 2024. In this post, I’ll try to predict three things for 2025.

  • Programming: Three Lessons Learned in 2024

    Programming: Three Lessons Learned in 2024

    Three lessons I’ve learned this year: 1. AI: Use it or see ya later It’s no surprise that is everywhere, but this year…

  • React 19: Server Functions

    React 19: Server Functions

    Server Functions are functions referenced on the client but executed on the server. Here’s an example: Check my earlier…

  • React 19: New hook useActionState

    React 19: New hook useActionState

    Usually, when working with a form, you’d want to: a) Display a loader b) Show validation errors This often means…

  • Javascript: Implementing Passwordless Login with Salesforce

    Javascript: Implementing Passwordless Login with Salesforce

    Salesforce offers a Headless Passwordless Login Flow that allows registered users to access an application seamlessly…

  • React: Implementing Passwordless Login with AWS Cognito

    React: Implementing Passwordless Login with AWS Cognito

    Passwords are easy to forget, and users often reuse the same password for everything. On the other hand, almost…

  • React: ReCAPTCHA v3 Client and Server Demo

    React: ReCAPTCHA v3 Client and Server Demo

    In this demo, I’ll use Google ReCAPTCHA v3 credentials within a React application built on Next.js.

  • React: LinkedIn Access Token in 10 Steps

    React: LinkedIn Access Token in 10 Steps

    I recently integrated with the LinkedIn API, and it turned out to be pretty straightforward. The task was to retrieve…

  • React + AWS Cognito: Email Authentication Setup Guide (Second Part)

    React + AWS Cognito: Email Authentication Setup Guide (Second Part)

    In the last post, we handled everything on the AWS side; now let's dive into React to set up our code. AWS provides the…

社区洞察

其他会员也浏览了