Host multiple site with node.js


I'm currently learning node.js and loving it. I noticing, however, that it seems that's it's really only fit for one site. So it's great for hosting, but what if I want to build an actual full web server with it. In other words, I would like to host,, and so on. What solutions (modules) are available for this? I was thinking of simply parsing the url from the request object and simply reading from the appropriate directory. For example if I get a request for then read from the example_com directory or if I get a request from read from the mydomain_com directory. The issue here is I don't know how this will affect performance and scalability.

I've looked into Multi-node but I don't fully follow the idea of processes yet (I'm a node beginner).

Any suggestions are welcome.

Problem courtesy of: selanac82


You can do this a few different ways. One way is to write it directly into your web application by checking what domain the request was made to and then route within your application but unless your application is very basic this can make it fairly bloated and can get messy. A good time to do something like this might be if you're writing a blogging platform where everything is pretty much the same across all your domains. The key difference might be how you query your data to display the right data.

In this case you'd probably use the request to see which blog is being accessed.

If you want to just host a few different domains on the same server all using port 80 (like most websites do) you will want to proxy each request off to a different process. You can do this with nginx or even with node itself. It all comes down to what best fits your needs. bouncy is a quick way to get setup doing this as its a nodejs module and has some pretty impressive benchmarks. nginx (proxy with nginx) is probably the most wildly used method though, as a lot of nodejs servers use nginx to serve static content anyways.

Solution courtesy of: travis


You can use connect's vhost middleware (which is also available in express) to dispatch requests to separate request handlers based on the Host: header. This assumes that everything is being handled by the same node process on the same port; if you really need separate processes, then the suggestion about using nginx as a reverse proxy is probably the way to go.

Discussion courtesy of: ebohlman

This recipe can be found in it's original form on Stack Over Flow.