Introduction
Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Installing Node.js got easier
Initially installing Node.js on the Raspberry Pi was a little tricky although there were a number of people providing instructions on how to compile it. Now though so long as you are using raspian and don’t mind using a slightly older version (v0.6.19 currently) you can install Node.js with apt-get.
sudo apt-get install nodejs
sudo apt-get install npm
Educational and practical
I think that Node.js presents some terrific practical and educational possibilities on the Raspberry Pi, learning or playing with JavaScript within the context of web-browsers on the device is not very practical as the browser and desktop software already take up a lot of system resources, node Js on the other hand provides a light-weight opportunity to experiment.
after installing ntwitter npm install ntwitter
for Node.js my Raspberry Pi
is tweeting it’s uptime hourly via the following script:
var twitter = require('ntwitter');
var twitterKeys = require('./twit_config.js');
var exec = require('child_process').exec;
var twit = new twitter({
consumer_key: twitterKeys.consumer_key ,
consumer_secret: twitterKeys.consumer_secret ,
access_token_key: twitterKeys.access_token_key ,
access_token_secret: twitterKeys.access_token_secret
});
//callback function for uptime twitter command.
function twitter_uptime_callback (error, stdout, stderr) {
twit.verifyCredentials(function (err, data) {
if (err) {
console.log("Error verifying credentials: " + err);
process.exit(1);
}
}).updateStatus('Raspberry Fool: my uptime and load: /' +
stdout + ' #rpi_status',
function (err, data) {
if (err) console.log('Tweeting failed: ' + err);
else console.log('Success!')
}
);
}
function tweet () {
child = exec("uptime", twitter_uptime_callback);
}
setInterval(tweet,3600000);
Note that this may not be the best way to do this I am building something that collects and collates tweets also. The credentials here are needed to use certain parts of the twitter api and are added here in a required file twit_config.js that simply contains a number of exports
exports.consumer_key = 'xxxxxx';
exports.consumer_secret = 'xxxxx';
exports.access_token_key = 'xxxxx';
exports.access_token_secret = 'xxxxxx';
To do something similar you will need to get twitter keys from dev.twitter.com.