Archive for the ‘node.js’ Category

Yet another must have node.js module – underscore-cli

September 12, 2012 Leave a comment

If you love underscore.js and happen to be a CLI nut, you should not miss out underscore-cli, see

for details, or simply install via (assuming you are already using node.js)

sudo npm install -g underscore-cli

Test Drive:
curl|underscore print --color

Categories: javascript, node.js

Install node.js on Raspberry PI in two simple steps

July 24, 2012 13 comments

[ Updated Jan 20, 2013 ] Check out my git repository if you are interested in putting newer version of node to raspberry pi (tested with 0.8.14, os runs raspbian)

1) Install the newest wheezy image onto the sd card (currently from


sudo apt-get install nodejs npm

, press y, Enter when prompted, you will get node.js (v0.6.19) and npm (1.1.4) installed in a few minutes, which should be the easiest way to get node.js and npm up and running on RP.

Note: If you want newer version of node.js you might want to check this out:, I have tested the method described in this post in the previous release of wheezy ( and I assume it should work with the current image as well.

Categories: node.js, raspberry pi

A simple password generator written in node.js

Probably not a very advanced password generator but should be adequate for general purpose, see for details.

Sheevaplug + Debian 6 (Squeez) + node.js 0.6.13

March 21, 2012 Leave a comment

1) Preparation

apt-get install build-essential libssl-dev curl
cd /tmp
tar xzf node-v0.6.13.tar.gz
cd node-v0.6.13

2) Edit file deps/v8/SConstruct as follows:

//line #80
  'gcc': {
    'all': {
      'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS', '-march=armv5t', '-mfloat-abi=soft'],
      'CXXFLAGS':     ['-fno-rtti', '-fno-exceptions'],

//line #1081
  'armeabi': {
    'values': ['hard', 'softfp', 'soft'],
    'default': 'soft',
    'help': 'generate calling conventiont according to selected ARM EABI variant'

3) Compile & Install

make && make install

Step 3) will take about 40 minutes. If installation is successful, command node -v should print v0.6.13.

4) Download and install npm

curl | sh
npm list -g
ln -s /usr/local/lib/node_modules ~/.node_modules

The ln command is optional but it’ll make require statements much easier.

Categories: Debian, node.js, sheevaplug

Example: sorted set functions with node.js + redis

February 28, 2012 2 comments

Have been playing with node.js module redis (and a bit of underscore) almost all day and I could not find any examples using zrange (or zrevrange) so I decided to write one as a practice:

var rc=require('redis').createClient();
var _=require('underscore');

rc.zincrby('myset', 1, 'usera');
rc.zincrby('myset', 5, 'userb');
rc.zincrby('myset', 3, 'userc');
rc.zrevrange('myset', 0, -1, 'withscores', function(err, members) {
        // the resulting members would be something like
        // ['userb', '5', 'userc', '3', 'usera', '1']
        // use the following trick to convert to
        // [ [ 'userb', '5' ], [ 'userc', '3' ], [ 'usera', '1' ] ]
        // learned the trick from
	var lists=_.groupBy(members, function(a,b) {
		return Math.floor(b/2);
	console.log( _.toArray(lists) );

Have fun.

Categories: node.js, redis, underscore

System load monitoring with

February 12, 2012 Leave a comment

I am a big fan of node.js, and a long list of other node modules, as you might have known through my News Board app. While working on a html5 charting project a few years back I discovered flot which I also became a fan of. I started a few weeks ago working on a real-time charting app with the technologies I just mentioned, by plotting some randomly generated numbers (streamed through with flot. Today I just changed the app by using real data – system average loads. Check out the project at github if you are interested in. My next goal is to use Redis to handle the data captured.[update] A new branch using Redis as data store has also been implemented.


Categories: flot, node.js,

A real-time app made with node.js,, express …

January 19, 2012 2 comments

This past weekend I decided to work on a pet project – build a real-time app using node.js.

Modules used:, express, jade, sqlite3.

Still screenshot:

News Board still image

News Board 3 clients on the left, admin UI on the right

Checkout the source codes at:

Video demo is available here (audio-less).

Have fun.

Categories: node.js,

[Fresh] Install node.js on Ubuntu server 11.10 (x64) via nvm step by step guide

January 8, 2012 2 comments
sudo apt-get -y install build-essential libssl-dev curl git
cd ~
git clone git://
. ~/nvm/
nvm install v0.6.7     # Current version as of this writing
nvm use v0.6.7
curl | sh      # Optional to install npm but recommended


  • It’s better to put the source command (. ~/nvm/ into your ~/.bashrc since the nvm command might get used quite often:
    if [ -f ~/nvm/ ]; then
        . ~/nvm/
  • If anything goes wrong during nvm install (for example, complaining certain library is not installed, I found the easiest way to re-start the install process is to install the missing library, followed by the removal of installation directory, ~/nvm/src/node-v0.6.7, for example
  • On my ubuntu vm running off an i3 laptop, it took 7 minutes to install node.js v0.6.7, YMMV
  • If you find yourself stuck inside node console, type command process.exit() to get out, solution found from here
Categories: node.js, nvm, ubuntu

A simple node.js rss parser using sax-js

November 15, 2011 Leave a comment

The xml parser sax-js written by Issacs (the creator of npm, the de facto package manager of node.js) comes with a few examples that deal with local xml files. I couldn’t find one that can parse xml data from remote host (think RSS) therefore I decided to write one. In this example I borrowed codes heavily from both sax-js’s example code and node-rss source code.

The codes

cat saxrss.js

var sax=require('sax');
var http=require('http');

var callback=function(){};

exports.get_rss=function(host,port,path, cb) {
	var parser = sax.parser(true)
	var item = null
	var currentTag = null
	var items=[]
	var cnt=0

	parser.onclosetag = function (tagName) {
		var tag_name=tagName.toLowerCase();
		if (tag_name === 'item' || tag_name === 'entry') {
			currentTag = item = null
		if (currentTag && currentTag.parent) {
			var p = currentTag.parent
			delete currentTag.parent
			currentTag = p

	parser.onopentag = function (tag) {
		if (tag_name !== 'item' && tag_name !== 'entry' && !item) return
		if (tag_name === 'item') {
			item = tag
		tag.parent = currentTag
		tag.children = []
		tag.parent && tag.parent.children.push(tag)
		currentTag = tag

	parser.ontext = function (text) {
		if (currentTag) {

	parser.onend = function () {

	var body='';
	http.get( { host:host, path:path, port:port }, function(res) {
		res.addListener('end', function() {
		res.on('data', function(d) {

cat test1.js

var rss=require('./saxrss.js');
var host='';
// to get finance headlines about stock AAPL
var path='/rss/2.0/headline?s=aapl&region=US&lang=en-US';

rss.get_rss(host, 80, path, function(items) {
To run

node test1.js

Required node modules:




[ UPDATE 2/20/2012 ]
With xml-simple module, the above example can be written as

// getting xml and convert to json object using xml-simple example
var http=require('http'), simplexml=require('xml-simple'), config= {host:'', path:'/rss/2.0/headline?s=aapl&region=US&lang=en-US', port:80}, body='';

http.get( config, function( res ) {
	res.addListener('end', function() {
		simplexml.parse(body, function(e, parsed) {
	res.on('data', function(d) {

To install xml-simple, simply npm install -g xml-simple.

Node.js: improve mysql query performance with mysql-pool and cluster

November 1, 2011 1 comment

Prior testing:

npm install mysql -g
npm install mysql-pool -g
npm install express -g
npm install cluster -g

Code that archives best result:

var express=require('express');
var cluster=require('cluster');
var MySQLPool = require("mysql-pool").MySQLPool;
var pool = new MySQLPool({
  poolSize: 10,
  user:     'node',
  password: 'node',
  database: 'testdb'

function symsearch(q, res) {
	var query=pool.query(
		   "select symbol,company_name from symbols where symbol like ? limit 10",
			function selectcb(err, results, fields) {
				if(err) {throw err;}
				console.log("Searching Result for "+q);
				res.send( results );


var app=express.createServer();
app.get('/sym/:q', function( req, res) {
	var q=req.params.q || '';
	if(q.length>0) symsearch(q, res);


Benchmark command:

ab -n 10000 -c 250 http://localhost:3000/sym/<pattern>

With the js code above, I am able to achieve 1300/sec on a dual-core 2.9GHz Virtualbox vm (ubuntu 10.10 32bit). When the same test is conducted on Apache2 + php5, I am getting about 800/sec. If I opt-for the single connection mode (use ‘mysql’ instead of ‘mysql-pool’), the result is only a little better than apache + php combo. The use of node module cluster is to take advantage of the dual-core cpu (node would run with in single-core mode due to its single-threaded nature).

Categories: benchmarking, mysql, node.js