Home > Web Front-end > JS Tutorial > How to Set Up a Node.js Web Server on Raspberry Pi

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

Linda Hamilton
Release: 2025-01-21 02:34:09
Original
990 people have browsed it

Setting up a Node.js web server on a Raspberry Pi: A step-by-step guide

A few years back, I acquired a Raspberry Pi Model B , and recently decided to transform it into a web server. This journey highlighted a key point: a Raspberry Pi functions as a miniature PC, requiring an operating system (OS). This contrasts with boards like Arduino, which execute programs directly without an OS.

Here’s how to build a Node.js web server on a Raspberry Pi from scratch:

  1. Install Raspberry Pi Imager:

This tool facilitates installing Raspberry Pi OS onto a microSD card. While alternatives like Debian or Ubuntu exist, Raspberry Pi OS is officially recommended for optimal performance.

  1. Install Raspberry Pi OS:

Launch Raspberry Pi Imager and configure these settings:

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

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

I used the default settings. Upon completion, you'll see a success message:

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

  1. Connect Peripherals: Insert the microSD card into your Raspberry Pi. Connect a mouse, keyboard, monitor, Ethernet cable, and power cable (power last is recommended).

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

Note the microSD card's location.

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

After booting, you'll see the desktop welcome screen:

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

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

Your Raspberry Pi OS is now ready.

  1. System Updates:

Open a terminal and run these commands:

<code class="language-bash">sudo apt-get update -y
sudo apt-get dist-upgrade -y</code>
Copy after login
Copy after login
  1. Install Node.js and npm:
<code class="language-bash">sudo apt-get install nodejs -y
sudo apt-get install npm -y</code>
Copy after login
Copy after login

Verify installation:

<code class="language-bash">node -v
npm -v</code>
Copy after login
Copy after login
  1. Install Express:

Express simplifies web server creation. I used the generator with default options:

<code class="language-bash">sudo apt-get update -y
sudo apt-get dist-upgrade -y</code>
Copy after login
Copy after login
  1. Install npm Packages:
<code class="language-bash">sudo apt-get install nodejs -y
sudo apt-get install npm -y</code>
Copy after login
Copy after login
  1. Run the Server:
<code class="language-bash">node -v
npm -v</code>
Copy after login
Copy after login

The server defaults to port 3000. Access it from another device using the Raspberry Pi's IP address (e.g., http://192.168.1.239:3000).

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

Your Express app is now network-accessible. Note that older models like the B might have slower processing times due to limited resources.

Conclusion:

The Raspberry Pi's versatility extends to IoT integration. While the B has limitations, newer models offer enhanced performance. The ability to run a Linux OS unlocks a wide range of package installations.

Extra:

Initially, I attempted Next.js, encountering a "JavaScript heap out of memory" error. Express proved a more suitable alternative for this older board. Remember resource constraints when choosing frameworks.

The above is the detailed content of How to Set Up a Node.js Web Server on Raspberry Pi. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template