How should I organize multiple Express servers on the same system?
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.
After a bit of research, I've found that I can use http-proxy 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:
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:
hostname.com/
app.js
routes/
views/
...
app1/
app1.js
routes/
views/
...
app2
...
hostname2.com/
app.js
routes/
views/
...
I'll have to run each app.js separately with node (or with forever, 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.
So, that's my current plan. What are the problems with it, and what pitfalls should I try to avoid? Most importantly, is there an established solution to this problem - the problem of hosting multiple web apps on the same system with Node.js/Express?
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.
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 this StackOverflow question
THIRD EDIT: I've come a ways since posting this question (though I have much further to go). Check out this file in my GitHub repository, which leverages what I learned from the answers to this question!
Since Express uses Connect, I'm pretty sure you can use Connect's virtual host middleware. It operates similar to other vhost modules on other products. I don't have multiple domains to test and show you proper code, but I would think it's something like this:
express.createServer()
.use(express.vhost('hostname1.com', require('/path/to/hostname1').app)
.use(express.vhost('hostname2.com', require('/path/to/hostname2').app)
.listen(80)
If you get to the point where one Express server isn't enough, then look into using the Node.Cluster from the API. If that also isn't enough, then the current practice is to put a asnyc reverse proxy such as Nginx in front of your Express servers and point the proxies to your Express servers.
If you don't need to use WebSockets (or any HTTP 1.1 feature, really), you can use NginX as your proxy instead.
The advantage is the total load NginX can handle versus Node is higher (being statically compiled and specialized for this sort of thing, basically), but you lose the ability to stream any data (sending smaller chunks at a time).
For a smaller site, or if you're unsure what features you'll need in the future, it's probably better to stick with node-http-proxy
and only switch to NginX if you can demonstrate the proxy is the bottleneck on your server. Fortunately NginX isn't hard to set up if you do need it later.