Archive

nodejs hosting amazon ec2

So I have finally fingered out nodejs hosting on amazon ec2 linux instances, and the end to end setup.

It is kind of sad that the hosting options are currently very limited, some of the hosting providers are still in privat beta, others are too expensive... but I am a testimony that it is not that hard to set up your own amazon ec2 nodejs server.

After you get through the learning curve, it is actually fairly easy... the scalability compare to the windows servers I am using with amazon ec2 is amazing... kind of never want to go back to .NET MVC any more... it so much faster to develop, deploy, change, scale...

installing nodejs on amazon ec2 linux

Here is what worked form me when I tried to installed node.js on a fresh EC2 Amazon linux instance.

wget http://nodejs.org/dist/node-v0.4.12.tar.gz
tar -zxvf node-v0.4.12.tar.gz
cd node-v0.4.12
yum groupinstall "Development Tools"
yum install make
sudo make install

The main issue that I had to figure out was that Development Tools and make was not installed, so this took me little bit to figure out, being a Linux novice ;)

This article is a good starting point, however it's missing the above details, if you just pick the default instance. Of course, you can browser through the community instances, and probably find something that has all the node.js stuff already installed.

Now going through the NPM installation, again a few hoops to jump, but figured it out:

sudo chown -R $USER /usr/local
sudo curl http://npmjs.org/install.sh | sh

Now I am ready to roll! ... wait, not quite, npm installed but is not actually working...

After reading around little bit, the fact that I build node without ssl support is not biting me back... so have to go back and reinstall...

sudo yum -y install openssl-devel

and now rebuild node again...

cd node-v0.4.12
sudo make install

ok now let's try it...

npm install lightnode

yea... it works... ready to roll now. In the meantime, I found this article, which seems to be more accurate, so in case my steps don't work, you can get some ideas here.

node.js mongoDb group by count

I am writing an app in node.js and starting to do some advanced data manipulation now. One thing I have not found in the mongoDb docs was how to do group by count query.

So here is my solution. (nodejs code right out of my array utility "class")

// Arry to object with defined keys.
exports.groupCount = function(array, key){
	
	var group = {};
	
	array.forEach(function(d, i){
		
		var keyName = d[key];
		
		if(group[keyName]){
			group[keyName].count++;
		} else {
			group[keyName] = {
				count: 1
			}
		}
	});

	return group;
};