0

I have written some code using express.js which I'd like to put on the same HTTP port as a web application implemented with another technology (in this case, Django). I don't want to have to redirect user browsers to another port since if I do they might bookmark URLs with the other port, and then I lose the ability to reconfigure the arrangements later. So, I'd like express.js to serve HTTP on its port, fulfilling some paths I specify by making HTTP requests to a secondary web application which is being served on another port.

Is there any middleware or other technique for express.js which will serve certain paths by making HTTP requests to other servers?

(The stackoverflow question How to make web service calls in Expressjs? is relevant but only discusses GET and I will have some POST requests to forward.)

Community
  • 1
  • 1
Dickon Reed
  • 3,575
  • 4
  • 23
  • 25

2 Answers2

2

Whilst it is possible to make POST request with node, I think the pattern you're describing is better suited to using a a server like nginx or apache in front of both node.js and django, and proxying requests to whichever port is appropriate based on the request.

Typically, both django and node.js would listen on whichever ports you want them to listen on, while nginx listens on port 80. You then define a virtual host in nginx that forwards certain requests to node.js and certain requests to django.

Here are the nginx docs on using proxy_pass.

Here is an example, modified from the nginx Full Example:

  server { # simple reverse-proxy
    listen       80;
    server_name  domain2.com www.domain2.com;

    # serve static files
    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /var/www/virtual/big.server.com/htdocs;
    }

    # pass requests for dynamic content to django
    location /djangostuff {
      proxy_pass      http://127.0.0.1:8080;
    }

    # pass requests for node
    location /nodestuff {
      proxy_pass      http://127.0.0.1:8081;
    }

  }
Stephen Emslie
  • 10,539
  • 9
  • 32
  • 28
  • I had wondered about that approach, but was concerned that socket.io might not work well through the proxy. I'll try it and find out. – Dickon Reed Aug 19 '11 at 15:10
  • From a little googling around it sounds like you need to compile nginx with tcp_proxy support. Presumably there is a similar module for Apache or your webserver. – Stephen Emslie Aug 19 '11 at 15:13
  • [this blog post](http://www.letseehere.com/reverse-proxy-web-sockets) seems to describe how to add tcp_proxy support to nginx. – Stephen Emslie Aug 19 '11 at 15:15
  • We tend to apache on my project, and I have more experience with that. After some work I have this basically start to work, except for a few 502 errors being logged by chrome which seem to be a common and expected. However, I am left feeling that exposing node and doing the proxying through that might be simpler. – Dickon Reed Aug 19 '11 at 16:42
  • It turns out socket.io doesn't work pretty well through proxies, for instance with issues such as https://github.com/LearnBoost/socket.io/issues/25. – Dickon Reed Aug 19 '11 at 20:20
1

With node-http-proxy only a single call to app.use is required to reverse proxy all unhandled requests, like this:

var app = express.createServer();
# my app.get bindings
app.use(require('http-proxy').createServer(80, 'other-server-address'));
app.listen(80);
Dickon Reed
  • 3,575
  • 4
  • 23
  • 25