Setting Up a HTTP Proxy Server using Node.js

Quick review of setting up a HTTP proxy server to front two separate projects. One containing just the UI code (html, js, css, etc.) and the other a Java Tomcat (Spring) based RESTful API backend.

I have a small project where I WANT to keep the Twitter Bootstrap based front-end code separate from the Java tomcat based RESTful API backend. My challenge is to allow the UI JS code to invoke the Java tomcat based RESTful services without worrying about the browsers same origin security policy. Using JSONP or CORS might get me some ways but I preferred the 3rd option where I did not have to make any code changes or server configuration changes. And this is to use a HTTP proxy to front the application while in development mode.

Here is a rough sketch of the design.

sketch-cl-arch

The gist of the solution and a diagram…

  • Run your services in server#1 (tomcat)
  • Run your UI code in server#2 (simple python web server)
  • Run a proxy server that will route the requests to one of the two servers. (node.js and http-proxy).
  • From your browser point to the proxy server.

proxy-cl-arch

Start the three servers now. The python web server simply serves up the web resources (html, css, js). The tomcat server serves up the RESTful API’s. Save the proxy code above into a file named server.js and start that using command – node server.js.

Once you have the three servers started you can hit the proxy at http://localhost:9090/app/index.html and you should be good to go.