Reverse proxy using ExpressJS

Reverse proxy is a proxy server which retrieve resources on behalf of client from one or more servers. Client end need not to know about all those servers. They request to proxy server on specific URL with over HTTP and proxy server finds out where to look ( in Servers ) to serve that request.

In this tutorial we will learn and develop proxy server using Express.js. Our proxy server will :

  • Contain 3 different server running on different port.
  • Accept requests and return response.


About http-proxy :

Http-proxy is a node module developed by Nodejitsu, one of the leading Node.js hosting provider. This module will help us to write Reverse proxy in Node.js very easily.

Project installation :

Create new Node.js project and create package.json file. It is recommended to use npm init command to generate one.

Once done use following command to install Express.js and http-proxy.

npm install --save express http-proxy

Server code

In order to run our reverse proxy server we need some resource server from which Proxy will fetch data. In order to do so, let’s develop three Express server running on Port 3001,3002,3003 respectively.

var express = require("express");
var app = express();
<code lang="javascript">

app.get(‘/app1’,function(req,res) {
res.send(“Hello world From Server 1”);


Copy paste same code for other servers too and change the text.

Proxy Server Code

Here is our simple proxy server code in express.js with multiple targets.

var express  = require('express');
var app      = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
var serverOne = 'http://localhost:3001',
ServerTwo = 'http://localhost:3002',
ServerThree = 'http://localhost:3002';
<code lang="javascript">

app.all(“/app1/*”, function(req, res) {
console.log(‘redirecting to Server1’);
apiProxy.web(req, res, {target: serverOne});

<code lang="javascript">

app.all(“/app2/*”, function(req, res) {
console.log(‘redirecting to Server2’);
apiProxy.web(req, res, {target: ServerTwo});

<code lang="javascript">

app.all(“/app3/*”, function(req, res) {
console.log(‘redirecting to Server3’);
apiProxy.web(req, res, {target: ServerThree});


You can add as many targets you want and it will create a proxy for that. In order to check whether its working or not we need to first run all the servers and hit request to /app1 and /app2 etc.

Running the app

Put all server in running mode, including proxy one. Note the servers are as follows :

==> localhost:3000 – Reverse proxy server.
== >localhost:3001 – First resource server.

Screen Shot 2015-12-06 at 12.17.20 am

You can hit the other server target URL too and it will return a response from second server.
Screen Shot 2015-12-06 at 12.18.04 am

Conclusion :

Reverse proxy is one of the famous approach when it comes to security. Real world user will never know from which server resource came from. Developing reverse proxy from core in Node.js is little tricky but thanks to awesome contribution as http-proxy, our life is easier.

17 thoughts on “Reverse proxy using ExpressJS”

  1. Great article and very useful. Thanks for sharing.

    Is this the same approach that big web applications uses to scale?

    1. Probably.

      Can’t say it by sure because they don’t share things like this in their doc 🙂

  2. Awsome work, love your articles.
    I tried the example and it works great when i have no further routes in the “subapp”. But when the subapp has routes of its own, it cannot match the route as the path is not at root level anymore. Is there a solution for this without touching the subapp?
    I have a react-router in one of the subapps, and it is very confused. 🙂

    1. I think you would want to take a look at mounting the subapp in Express using mountPath.

  3. Nice! Is it possible to add routes dynamically without restarting the router? Thanks in advance!

  4. Great and useful article ! Is it possible for the proxy server to load static pages ? Like if we have an HTML client and we want to make a request to one of the servers via that client.

    1. Shahid (UnixRoot) Shaikh


      Basically it will forward the HTML encoded content coming from server to proxy to client.

  5. Hi,

    I tried using the exact code, but for requests like POST, it converts it to GET, can you suggest something for think.

    Thanks in Advance.

  6. Thanks for info. Wondering how reverse prixy is it different than proxy ? Or its the same thing.

  7. Shahid, I am working on a project where it get the data from an external API and it is in JSON format, I need to customize it to our requirement, that only need 1% of the data we received. Is there a way we can rewrite the output? I tried Harmon Trumpet and its meant for html tag manipulation. What API should I use, can you put a tutorial on that? Thank you

  8. Very nice and simple explanation. I could see some copy paste error in proxy server code, redirecting to server3 URI is app2 and not app3. So this code never hits server3.
    Please update that. Except that everything looks very good.

  9. Thanks for sharing.

    Can you explain how to deal with HTTP -> HTTPS & HTTPS -> HTTPS scenario with above example ?

Comments are closed.