Node.js is a brilliant platform for creating network applications. It is mainly known for its non-blocking I/O and event driven system. In simple terms, Node.js can easily handle a large number of requests while simultaneously consuming lesser server memory. These are the attributes one should be looking for in a low power server like, you guessed it, Raspberry Pi. Here, you’ll learn how to setup Node.js for Raspberry Pi.

Node.js

ON

Raspberry Pi

Why use Node.js?

Node.js comes with a built-in HTTP server library. This means it doesn’t require the help of any external piece of software to act as a web-server. Using Node.js alone one can have greater control of the web-server parameters.

Of course, all we care about it tinkering. Cool new web applications require Node.js to function, and we like our Raspberry Pi not being a source of heat all the time. Of course, you could check the Wikipedia entry as well as their very own site.

Installing Node.js on Raspberry Pi

1. Download the Node.js package for ARM

As you must be aware, the Raspberry Pi sports an ARM11 chip. So, the package optimised for ARM will have to be downloaded. It can be done by entering the following command into the terminal.

<code>wget http://node-arm.herokuapp.com/node_latest_armhf.deb</code>Code language: HTML, XML (xml)

2. Install the package

Once the download is complete, it needs to be installed and can be done using the following command.

<code>sudo dpkg -i node_latest_armhf.deb</code>Code language: HTML, XML (xml)

And that’s it! Quick, wasn’t it?

By its end, we will have NodeJS and NPM (Node Package Manager) installed on your Raspberry Pi.

Testing the installation

The process is pretty foolproof but it wouldn’t hurt to test the installation with a simple server script.

1. Write the server script

Below, we have written a script that displays “Hello World!” to the client.

var http = require('http');
http.createServer(function (request,response) {
 response.writeHead(200, {'Content-Type': 'text/plain'});
 response.end('Hello World!\n');
}).listen(8000)
console.log("Web Server running at http://127.0.0.1:8000")
Code language: JavaScript (javascript)

Update: There’s a chance the code might not appear in with proper line-breaks on your PC. So, here below we’ve enlisted the above code in its right syntax:

Save the script in a file

Save the script in a .js JavaScript file. Something like greetings.js would be a good idea if you’re not feeling particularly creative.

2. Run the script

Use the node program from the command line to execute the server. Something like so.

node greetings.jsCode language: CSS (css)

You’ll receive an output like this.

Hello World!

If you’d like you could visit http://127.0.0.1:8000 and be greeted “Hello World!”.

And that is that!

Stay tuned for more on Raspberry Pi, Node.js and all of the web-apps based on it. Do leave us comments if you’d like us to do anything for you…

I compiled a list of software and services that I use to improve my workflow, here is the link to the list.

Darryl Dias

I’m Darryl. I’m a 3D Artist, Programmer and Linux enthusiast. On this site I share my insights, tips and tricks, tutorials, methods and best practices.