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


In this post we build and install Node.js from source. Next, we install npm, the package manager for nodejs. Using npm we install two other packages, an HTTP middleware called connect, and a reverse proxy server called http-proxy. We use this setup to replace Apache httpd in the web chat post. Ubuntu Linux is used throughout.

Obtain and install Node.js

Install the libssl-dev package of openssl, if not installed

sudo apt-get install libssl-dev

Download Node.js source, extract it using

tar xzvf <filename>

Enter the source folder and execute the following commands in sequence

./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 package curl and its dependencies, if not already installed.

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 both these packages. npm installs these packages in the node_modules folder relative to the current folder. The node_modules folder needs to be in the NODE_PATH environment variable, otherwise node will not find it. Another option is to move the contents of the node_modules folder to ~/.node_modules. This is where node searches for modules by default. We’ll install to the global location at /usr/lib/node_modules (-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

Run connect and http-proxy

Create a file with extension js (e.g. 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);

Now, we can run Node.js

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

Configure trophyim

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

var TROPHYIM_BOSH_SERVICE = 'http://localhost/http-bind/';  // Change to suit

If you have done everything correctly, you can access http://localhost/trophyim.0.3/index.html from the browser and chat with any contact in your roster.

One thought on “Node.js to serve static content and as an http proxy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s