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 node: No 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
at this line ""npm install --save compression connect http serve-static"" it shows me a lot of errors why ? help please , thanks!
ReplyDeletemaybe you don't have enough rights, try to execute it as sudo
DeleteIf you have Raspberry Pi Model A, B, B+ read this
ReplyDeletehttp://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