HTTP Proxy – node.js

What is HTTP Proxy basically?

In computer networks, a proxy server is a server (a computer system or an application) that acts as an intermediary for requests from clients seeking resources from other servers. A client connects to the proxy server, requesting some service, such as a file, connection, web page, or other resource available from a different server and the proxy server evaluates the request as a way to simplify and control its complexity. Proxies were invented to add structure and encapsulation to distributed systems.

Also called/used as “HTTP Proxy is basically a webaddress you type in to your companies proxy server so you can access the internet.”

What does it do?

Proxy server advantages include:

  • Maintaining identity anonymity as a security precaution.
  • Content control
  • Accelerating caching rates.
  • Facilitating access to prohibited sites.
  • Enforcing access policies on certain websites.
  • Allowing a site to make external server requests.
  • Avoiding security controls.
  • Bypassing Internet filtering for access to prohibited content.

So what it does in nodejs?

An HTTP proxy with custom routing logic that conditionally forwards to different ports based upon the requested host and path name.

Install http-proxy and url:

$ npm install http-proxy url

Then define the proxy as a node.js instance (we’ll name the file proxy-server.js):

var httpProxy = require("http-proxy");
var url = require("url");

httpProxy.createServer(function(req, res, proxy) {

  var hostname =":")[0];
  var pathname = url.parse(req.url).pathname;

  // Options for the outgoing proxy request.
  var options = { host: hostname };

  // Routing logic
  if(hostname == "") {
    options.port = 8083;
  } else if(pathname == "/upload") {
    options.port = 8082;
    options.path = "/"; 
  } else {
    options.port = 8081;
  // (add more conditional blocks here)

  proxy.proxyRequest(req, res, options);


console.log("Proxy listening on port 8080");

All incoming requests on port 8080 will be received by the proxy and considered by the routing logic. The url module is used to parse and extract thepathname, which is tested along with the hostname to determine the forwarding target.

Requests with as the host name will be forwarded to localhost:8083. Requests that do not have as the host but have the path/upload are forwarded to localhost:8082. Any other request will be forwarded the port localhost:8083.

You can add this to the bottom of proxy-server.js:

var http = require("http");

http.createServer(function(req, res) {
  res.end("Request received on 8081");

http.createServer(function(req, res) {
  res.end("Request received on 8082");

http.createServer(function(req, res) {
  res.end("Request received on 8083");

Start the proxy by running in the terminal:

$ node proxy-server.js
Proxy listening on port 8080

Now assert the routing works as expected using your browser.

This should forward to the server listening on port 8081:

Request received on 8081

The /upload path will forward to 8082:

Request received on 8082

Entering as the host name forwards to 8083:

Request received on 8083

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