2

I am setting up proxy server using NODE and EXPRESS. I have the same setup/codebase in local and proxy server. But i want to use all the js,css,images and other static content from local machine and json response from proxy server. Now since the proxy server also has same ,js,css,images, it is picking up everything from proxy. Do I need to restrict urls calls to not pick js,css,images and other static content from proxy but from local. How do i do that? Here is the code structure (both in local and proxy)

 /src
  /javacode

/WebContent
  /js
  /css
  /images
  /jsp

I want everything under /WebContent to be used from local. This is how i setup proxy:

var proxy = httpProxy.createProxyServer();

    app.route('/app/*$').all(function (req, res) { // proxy all requests

        proxy.web(req, res, {target: 'http://proxy-server:7001'}); //sandbox
    });
isherwood
  • 58,414
  • 16
  • 114
  • 157
McQueen
  • 460
  • 2
  • 9
  • 20
  • For the static content you want served from your local machine, can it all be served from a common path root, eg `/public`? – Andrew Lavers May 04 '15 at 15:25
  • eah I create a public folder and put all content from /WebContent into it...will that work? By the way in this case, isn't '/WebContent' same as '/public'? – McQueen May 04 '15 at 15:35

1 Answers1

2

Given your file structure, you can use express.static to map your static /WebContent dir to a WebContent virtual path, like this:

var proxy = httpProxy.createProxyServer();

app.use('/app/js', express.static('WebContent/js'));
app.use('/app/css', express.static('WebContent/css'));
app.use('/app/etc', express.static('WebContent/etc'));

app.route('/app/*$').all(function (req, res) { // proxy all requests
    proxy.web(req, res, {target: 'http://proxy-server:7001'}); //sandbox
});
Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50
  • I tried that but it still pick up static content from proxy server may be because the urls route that I have setup falls under that criteria? – McQueen May 04 '15 at 16:00
  • Ah I think I had a mistake in my code sample. I should have been passing the dir name directly to the static method. I've updated it, give that a try. – Andrew Lavers May 04 '15 at 16:16
  • basically my application is deployed with name 'app' and all the static content are under /WebContent which is a root folder. So when I make call, it looks for js with url '/app/js/file.js' (same for css , images or other static content) and since this url passes the route criteria ('/app/*$'), it picks the content from server and not from local – McQueen May 04 '15 at 17:04
  • So you would like the route `/app/WebContent` to serve your local content? – Andrew Lavers May 04 '15 at 18:18
  • no 'app' is the application name when it is deployed and it looks for all the resources under root fiolder which /WebContent – McQueen May 04 '15 at 19:55
  • Ah I see. You can still get what you want but you'll have to map each resource type to the specific route (eg js-->/app/js). You need to do this in order to distinguish the static routes from your proxy route. I've updated my answer inline. – Andrew Lavers May 04 '15 at 21:15
  • Thanks, i tried doing this too but it still is picking up js files from PROXY server not from LOCAL. – McQueen May 05 '15 at 12:55