title: How should I organize multiple Express servers on the same system?
body: <p>I'm using one server to host multiple Node.js web apps, which are distributed across multiple domains. My current practice is to run an Express server for each app on a different port, and to run a base server that simply routes (redirects) requests to the correct port/Express server. This works, but it means that my base server is routing every single HTTP request (and by manually redirecting it), and that my users see my apps as hosted at [hostname.com]:8000.</p>
  
  <p>After a bit of research, I've found that I can use <a href="https://github.com/nodejitsu/node-http-proxy" rel="noreferrer">http-proxy</a> for my routing needs, but I'd still like to know if there's a best practice for running multiple Express servers on the same system. Here's how I'm planning on doing it:</p>
  
  <p>Each web app will have its own folder, with a complete Express folder structure (app.js, routes, views, etc.) Apps will be grouped by domain, so an example folder structure would be:</p>
  
  <pre><code>    hostname.com/
          app.js
          routes/
          views/
          ...
          app1/
              app1.js
              routes/
              views/
              ...
          app2
          ...
      hostname2.com/
          app.js
          routes/
          views/
          ...
  </code></pre>
  
  <p>I'll have to run each app.js separately with node (or with <a href="https://github.com/nodejitsu/forever" rel="noreferrer">forever</a>, which I'm currently using), and each one will have to use a different port internally, with cross-app redirects being pointed at the port of the target app.</p>
  
  <p>So, that's my current plan. What are the problems with it, and what pitfalls should I try to avoid?
  Most importantly, <em>is there an established solution to this problem - the problem of hosting multiple web apps on the same system with Node.js/Express?</em></p>
  
  <p>EDIT: I do plan to eventually use WebSockets and HTTPS, and the amount of bandwidth my setup can support is of little importance to me - this is a development server (at least for now). Thanks to David Ellis for bringing up the issue of WebSockets.</p>
  
  <p>SECOND EDIT: Thanks to both EhevuTov and David Ellis for their answers, both of which helped greatly. I'm still settling on an overall structure for my application, and it looks like that question is addressed in some detail by <a href="https://stackoverflow.com/questions/5778245/expressjs-how-to-structure-an-application">this StackOverflow question</a></p>
  
  <p>THIRD EDIT: I've come a ways since posting this question (though I have much further to go). Check out <a href="https://github.com/airandfingers/rpwm-server/blob/master/bootstrap.js" rel="noreferrer">this file in my GitHub repository</a>, which leverages what I learned from the answers to this question!</p>