I finish writing a HTTP Proxy Server using NodeJs.
NodeJs is a non–blocking event-driven I/O framework for the V8 JavaScript engine (The Chrome JS engine).
Clarifying, nothing is blocking in NodeJs. Everything is handled by events. This feature provides networking applications that can handle a thousands of request and do more stuff at same time!
On my experience, complex and critical applications, like web servers, run so fast on NodeJs.
Let’s take a look at my HTTP Proxy Server Features.
- A Complete Proxy Server
- Written over Node JS framework
- Customizable request handlers
- Javascript to write server side code!
- Easy configuration to handle all browsers
Setting up the Server
To Run the server you need to install nodejs first:
Just go to http://nodejs.org/ and download the latest version. Then go to https://github.com/joyent/node/wiki/Installation and follow the instructions to install node.
Then you can checkout the proxy:
svn checkout http://node-proxy-server.googlecode.com/svn/trunk/ node-proxy-server-read-only
Next, open a terminal, navigate to the proxy directory and type:
~$ node proxy.js
Now the server is running on localhost:8000.
It’s time to setup your browser to listen it. Do the follow (On Mozila FireFox):
- Go to Edit > Preferences on the menu bar
- In the tab “Advanced” Select “Network” and click on “Settings”
- Select the option “Manual proxy configuration”
- In the textbox HTTP Proxy write: localhost and set the port to 8000.
- Click on Ok and it’s done! You’re Ready to Browse.
Extending the server
You can extend your server adding request handlers in the handlers.js file.
A handler must be an object that contains a ‘pattern’ (string which matches the urls) and an ‘action’ (function that indicates what to do with the request). For example:
var handler = {
pattern : 'facebook',
action : function(response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end("Hello facebook!");
}
}
The handler must be in a list of handlers that are exported to the server module:
exports.handlers = [handler];
That’s all! If you want to write more complex modules, you can read the node js documentation (http://nodejs.org/docs/v0.4.3/api/http.html) or contact me.
Check out the repository
svn checkout http://node-proxy-server.googlecode.com/svn/trunk/ node-proxy-server-read-only
Enjoy the code!