/node-js/

Raspberry Pi2 and NodeJs

2015-05-17 19:49:45

There is a plenty of interesting projects based on RaspberryPi.

Today we will learn how to start with Node.js

First step. Node.js instalation

wget http://node-arm.herokuapp.com/node_latest_armhf.deb 
sudo dpkg -i node_latest_armhf.deb

btw. for that tutorial you have to have ssh access to your raspberry pi, more about that you can find at the end of the article

test your node after installation

node -v

if you see something link this v0.12.1, everything is ok

Second step. Write small Express app

app.js

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.send('Hi From RaspberryPi');
});

var server = app.listen(3000, function () {

    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);

});

package.js

{
  "dependencies": {
    "express": "~4.12.3"
  }
}

run command

npm install

run app

node app.js

ok if everything works (it should :-) ) you have to send this app to your raspberry, you can use FileZilla. After upload, you can run app on raspberry, simply typing node app.js

Now, go to browser and type: http://192.168.3.100:3000/ (of course you have to use correct IP)

SSH Access, run command (linux)

if you know IP of your raspberry you can easily log in from other commuter

$ ssh pi@192.168.3.100
pi@192.168.3.100's password: raspberry