How to write an express node application that serves most local files but redirects some of them to another domain?

I am working on a small webapp, which is usually created with a relatively complex process and then deployed to WebLogic.

However, the part I'm working on uses AngularJS, and all of this is HTML and Javascript. Usually he makes ajax calls in another webapp in the same domain. To shorten the development cycle, I would like to avoid the build process and just reload the browser page.

I think I can do it with <node express ", but the details will go away from me. I was able to define a very simple application that just serves local files, but now I need to figure out how to define some of these paths as matching the expression, and forward these request requests to an external domain.

So, if he receives a request for "/diag/stuff.html", "/foo/thing.html" or just "/index.html", he will send back the file corresponding to the same path. However, if the path matches "/fooService/.*", then I must send a response from GET to the same path, but to a different host and port.

This is my trivial application:

var express = require('express'); var app = express(); app.use("/", express.static(__dirname)); app.listen(8000); 

Update:

I like the idea of ​​a proxy, so I did a local installation of "http-proxy" (I forgot and did a global installation first), and then changed the script to this:

 var express = require('express'); var app = express(); var httpProxy = require('http-proxy'); var proxy = new httpProxy.RoutingProxy(); app.use("/", express.static(__dirname)); app.get('/FooService/*', function(req, res) { "use strict"; return proxy.proxyRequest(req, res, { host: "foohost.net", port: 80 }); }); app.listen(8000); 

This fails:

 <path>\server.js:4 var proxy = new httpProxy.RoutingProxy(); ^ TypeError: undefined is not a function at Object.<anonymous> (<path>\server.js:4:13) 

What could be wrong here?

Update:

It would be helpful to see the contents of "console.log (httpProxy)" after that "require" ?:

 function ProxyServer(options) { EE3.call(this); this.web = this.proxyRequest = createRightProxy('web')(options); this.ws = this.proxyWebsocketRequest = createRightProxy('ws')(options); this.options = options; this.webPasses = Object.keys(web).map(function(pass) { return web[pass]; }); this.wsPasses = Object.keys(ws).map(function(pass) { return ws[pass]; }); this.on('error', this.onError.bind(this)); } 

Does this mean why the "new httpProxy.RoutingProxy ()" says it is undefined?

+1
source share
2 answers

You can use http-proxy and forward requests to another host. To install http-proxy , you need to run sudo npm install http-proxy . The code that will handle the proxy will look like this:

 httpProxy = require('http-proxy'); proxy = new httpProxy.RoutingProxy(); (...) app.get('/fooService/*', function (request, response) { "use strict"; return proxy.proxyRequest(request, response, { host : externalHost, port : 80 }); }); 

UPDATE

The code works http-proxy ~ 0.10.x. Since then, a lot has changed in the library. Below you can find an example for the new version (at the time of writing ~ 1.0.2):

 var httpProxy = require('http-proxy'), proxy = httpProxy.createProxyServer({}); (...) app.get('/fooService/*', function (request, response) { "use strict"; return proxy.web(request, response, { target: 'http://fooservice.com' }); }); 
+6
source

If the redirects fit your needs, then this is the easiest solution:

 var express = require('express'); var app = express(); app.use(express.static(__dirname + '/public')); app.use(app.router); app.get('/fooService/*', function(req, res){ res.redirect(302, 'http://otherdomain.com:2222' + req.path); }); app.listen(8000); 

Note that it is generally considered good practice to use a subdirectory for your static files (e.g. public above). Otherwise, you could view your application file and everything that remained at the root of your application!

0
source

All Articles