Beginners guide to node.js, socket.io and the express framework - Installation

After spending several hours working out how to get the basic node.js, socket,io and express demo working I learned a few things. Here is what I learned.
  • Rev version matters. Make sure that the correct version of each program is installed.
  • All the programs installed with npm need to be installed in the same directory as the node.js files.
  • Most tutorials assume that the basic developer tools are installed, eg. g++.
  • The nvm program works for choosing node.js versions to run, but does not tell the socket.io or express packages that an earlier version of node is in use.
  • The tutorial I followed can be found at http://psitsmike.com/2011/09/node-js-and-socket-io-chat-tutorial/
My server runs Ubuntu 12.04 LTS and I have installed g++, git, curl, libssl-dev, and apache2-utils as well as some other commonly used packages (let me know if I missed one). If you don't have a github account, then you should set one up, or you can download the .zip from the github project page and unzip and move it to the installation folder you choose (such as ~/Documents/node). Run the following commands in your bash terminal to get going.
sudo apt-get install g++ curl libssl-dev apache2-utils

sudo apt-get install git-core

cd /pick_a_folder #~/Documents/node is a good choice

git clone git://github.com/joyent/node.git

cd node

git checkout v0.10.19 #or latest stable version

./configure

make

make install #finish nodejs

Edit: Newer versions of node.js are bundled with npm, so installing npm may not be necessary.

Then install npm to make it easy to add modules to node.js.
curl -s https://npmjs.org/install.sh > npm-install.sh | sudo sh npm-install.sh
You can verify your installations by entering node -v and npm -v you should see the version numbers for each. Now I installed socket.io by following the directions on their site. Socket.io handles most of the websocket functions for you and also chooses the best transport methods.
npm install socket.io
And then I installed the express framework. The express framework takes care of the webserver portion of the project. I used the -g option so that it will work with forever.
sudo npm install -g express
Next I install forever so I can close my terminal after starting node.js programs. It also takes care of handling errors and generally keeping the node.js program running.
sudo npm install forever -g

That concludes the installation of node.js and the necessary modules to create a usable application. Next run a simple chat server or checkout the real-time web based HMI

Read More