How to Build a Proxy Server using Node.JS

Maybe your school or workplace's website blockers are a bit heavy handed, or you are web developer who wants to bypass most browser's security settings and do some fancy cross-domain scripting, or perhaps you are someone who wants to hide their web traffic. Regardless, a web proxy can be useful in accomplishing many otherwise impossible tasks.

Before we begin, it would be useful to know generally what a proxy is, what it does, and how it does it. In short, a proxy server is a server that acts as an intermediate stage in communications, effectively removing any direct communication between two or more computers. This means that computer A can send a message to computer B by first sending the message to a proxy server, and having the proxy relay the message to computer B. A simplistic representation of this effect can be seen in the following diagram:

In order to create this effect, we will simply design a server-side program that takes a url in the form of a url parameter (more about url parameters), downloads the requested file, and forwards the result to the user.

To do this, we will be using Node.JS. Before proceeding, you should have node installed on your local machine. The installation can be found here. Although this tutorial is designed for programmers, if you are not a programmer, and simply want to bypass website blockers, you should be able to get by with installing node and following this guide as closely as possible.

To begin, we will build a simple proxy which simply forwards the initial request through the proxy. Although this is exactly what you need for many purpases, it will cause problems on most complectated websites. If you are feeling impatiant, feel free to skip to the more advanced section, where many of these problems will be addressed.

Simple Web Proxy

First we need to create a directory for this project. For our purposes, I will be calling it 'proxy'.

Next, we need to require in express and request, which are the two modules we will be using to make this easier.

            cd proxy
            npm install experess
            npm install request

In the proxy folder we will also create a file called proxy.js, which will house all of the server's logic. This can be done using any text editor, but the one that I will be using is sublime text 2.

Now that we have finished the prep work, we will begin the codding.

First, we will require the needed modules:

                var express = require('express'),
                app = express(),
                request = require('request'),
                fs = require('fs'),
                url = require('url');

Next, we will initialize our server, and have it listen at port 3000. We will also use app.get('*' to have the server take care of any request.

                console.log('Listening on port 3000');
                app.get('*', function(req, res) {
                    //A function that takes a url and downloads the source.
                    //This will be defined in the next step.
                    getSource(req.params[0].substring(10), res);

At this point, if we start he server by calling 'node proxy' from the command line, it should print: 'Listening on port 3000'.

Next, we will define the getSource function. This function will take a url in the form and will use 'req' to send the result back to the user.

            function getSource(uri, res) {
                //Get the source of the url.
                request(uri, function(error, response, body) {
                    if (!error && response.statusCode == 200) {
                        res.send("An error Has occured, the url was most likly incorrect.");

And that's all! If you now open the console and type 'node proxy', then open your browser and go to localhost:3000/ you should get the web page returned to your window.

Advanced Proxy

Still a work in progress, will be online shortly.