View on GitHub

Devendra's Log

Node.js to serve static content and as an http proxy

In this post, we use Node.js and some additional packages, on Ubuntu Linux, to replace Apache httpd used in the post Web chat using Strophe and Openfire.

First, we build and install Node.js from source. Next, we install npm package manager for Node.js. Using npm, we install two other packages - an HTTP middleware called connect, and a HTTP reverse proxy called http-proxy.

Obtain and install Node.js

Install the libssl-dev package of openssl

sudo apt-get install libssl-dev

Download source code of Node.js and extract it using

tar xzvf <filename>

Enter the source folder and execute the following commands

./configure --prefix=/usr
make
sudo make install

Obtain and install npm

npm can be built from source, but we recommend using the following command. You will need to install curl and its dependencies.

sudo apt-get install curl
curl http://npmjs.org/install.sh | sudo sh

Obtain and install connect and http-proxy

We use npm to obtain and install packages to the global location at /usr/lib/node_modules using the -g switch. http-proxy depends on a package called eyes so we install that as well.

Execute the following commands

sudo npm -g install connect@latest
sudo npm -g install http-proxy@latest
sudo npm -g install eyes@latest

Setup connect and http-proxy

Create a file run.js containing the following Javascript code. It initializes http-proxy to proxy requests to the Openfire BOSH service, and the HTTP service provided by connect. It then initializes connect to serve static content.

var options = {
  router: {
    'localhost/http-bind/': 'localhost:7070',
    'localhost/': 'localhost:8081'
  }
};
httpProxy = require('http-proxy');
httpProxy.createServer(options).listen(80);

var connect = require('connect');
connect.createServer(
  connect.favicon(),
  connect.static('/home/login/work/')
).listen(8081);

Configure trophyim

Copy the folder of trophyim web chat client to /home/login/work. We need to modify the TROPHYIM_BOSH_SERVICE var in trophyim.js.

var TROPHYIM_BOSH_SERVICE = 'http://localhost/http-bind/';

Now, run Node.js

export NODE_PATH=/usr/lib/node_modules/
node run.js

Node.js will not search a node_modules folder for packages if it does not appear in the NODE_PATH environment variable.

If you have done everything right, you should now be able to access http://localhost/trophyim.0.3/index.html from the browser, and chat with any contact in your roster.