title: NodeJS - Reverse Proxy with Route Changing
body: <p>I'm currently using NodeJS/Express as a simple domain router running on my VPS on port 80. My routes.coffee looks something like this:</p>
  
  <pre><code>request = require("request")
  
  module.exports = (app) -&gt;
  
      #404, 503, error
      app.get "/404", (req, res, next) -&gt;
          res.send "404. Sowway. :("
  
      app.get "/error", (req, res, next) -&gt;
          res.send "STOP SENDING ERRORS! It ain't cool, yo."
  
      #Domain Redirects
      app.all '/*', (req, res, next) -&gt;
          hostname = req.headers.host.split(":")[0]
  
          #Website1.com
          if hostname == 'website1.com'
              res.status 301
              res.redirect 'http://facebook.com/website1'
  
          #Example2.com
          else if hostname == 'example2.com'
              pathToGo = (req.url).replace('www.','').replace('http://example2.com','')
              request('http://localhost:8020'+pathToGo).pipe(res)
  
          #Other
          else
              res.redirect '/404'
  </code></pre>
  
  <p>As you can see in Example2.com, I'm attempting to reverse proxy to another node instance on a different port. Overall it works perfectly, except for one issue. If the route on the other node instance changes <em>(Redirects from example2.com/page1.html to example2.com/post5)</em>, the URL in the address bar doesn't change. Would anyone happen to have a nice workaround for this? Or maybe a better way to reverse proxy? Thanks!</p>