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
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 | 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');

var connect = require('connect');

Now, we can run Node.js

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

Newer versions of node, don’t require NODE_PATH to be set to the global module path used by npm -g i.

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: Logo

You are commenting using your 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