WHAT'S NEW?
Loading...

How to install Node.js on Raspberry Pi 2 and create a simple Webserver to Host a static Website

When you work to build a modern website based only on HTML5 and Javascript, you don't need complex configurations to verify how the end user will display the web site. A simple web server serving static files is the fastest and easiest solution, in this guide we will learn how to install Node.js and create a simple web server to use for our work. Node.js is a runtime environment for developing server-side web applications. In simple words, the Node.js platform makes it possible to run JavaScript programs on a server and also on your Raspberry Pi 2.



First of all, please run the following command in a shell or terminal

sudo apt-get update

Please wait! This command can be very slow, it downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies.

Remove a program with similar name to node.js (it creates conflicts, so better remove it)

sudo apt-get --purge remove node

To install Node.js run the commands

sudo apt-get install nodejs
sudo apt-get install npm

Nice job! You have sucessfully installed nodejs and npm in your Raspberry Pi 2. You can see the version currently being used by the shell by typing:

node -v

If nothing happens and you have the error  nodeNo such file or directory create a symlink with the command

sudo ln -s /usr/bin/nodejs /usr/sbin/node

Create a folder called /var/www/ and navigate to that folder, it will be our work directory.

sudo mkdir /var/www 
cd /var/www

We are ready to start coding! We will create a simple Webserver to host static website. Install the dependencies 

npm install --save compression connect http serve-static

Create a folder called httpdocs, this folder will cointain your HTML file, images and js code you want to host.

sudo mkdir /var/www/httpdocs

For example create a file index.html

Create in /var/www a file named server.js and put this code inside:

or you can download the file

wget https://gist.githubusercontent.com/orfeomorello/704f8639a5afcf50871f/raw/2ca571e41e9bc4c2fd12578293bdcd08eda51a34/server.js

If you followed all the steps, typing the ls command you can see this dicrectory structure

httpdocs node_modules server.js

To start the web server, execute the script with the command

node server.js

This will create a local webserver running on port 8181, if you write this web addess in your browser http://localhost:8181/index.html you will view your website running (remember to put your files in /var/www/httpdocs folder). To stop running the server press ctrl + c

Do you want to make available your website directly on the Internet? Follow this other tutorial

3 comments: Leave Your Comments

  1. at this line ""npm install --save compression connect http serve-static"" it shows me a lot of errors why ? help please , thanks!

    ReplyDelete
    Replies
    1. maybe you don't have enough rights, try to execute it as sudo

      Delete
  2. If you have Raspberry Pi Model A, B, B+ read this

    http://blog.wia.io/installing-node-js-v4-0-0-on-a-raspberry-pi/

    wget https://nodejs.org/dist/v4.0.0/node-v4.0.0-linux-armv6l.tar.gz

    tar -xvf node-v4.0.0-linux-armv6l.tar.gz

    cd node-v4.0.0-linux-armv6l

    sudo cp -R * /usr/local/

    node -v

    ReplyDelete