I have a site for hosting my dev projects. Each project is a node.js
file. The problem is I can only have 1 project online at the same time - except if I host them in a different ports. But suppose I want to publish 2 projects like that: my_site.com/foo
, my_site.com/bar
, where the first is managed by "foo.js" and the second by "bar.js". How can I do that?

- 51,090
- 44
- 144
- 286
-
maybe to read http://stackoverflow.com/questions/10216003/how-to-make-node-js-multi-tenant-for-websites-on-port-80?rq=1 could give you a help, or a start point, however.. – Ragnarokkr Apr 29 '13 at 01:01
3 Answers
You need a proxy in front. You assign each separate node process a different port. The proxy forwards traffic on port 80 to the appropriate backend node process.
This can be accomplished with node-http-proxy (https://github.com/nodejitsu/node-http-proxy), or any web server. Nginx and lighttpd make it ridiculously easy, Apache less so but still completely doable.

- 41,122
- 56
- 157
- 219
-
That is almost perfect, but then I would have to create the processes on different ports. That'd be a little confusing, because if I ever wanted to run "foo.js" alone (say, I just sold it to someone) I wouldn't be able to just "node foo.js". – MaiaVictor Apr 29 '13 at 01:16
-
2Its not a matter of almost perfect, its the only way to do it. You can't have multiple processes running on the same port. Its a low-level system thing. – Geuis Apr 29 '13 at 01:24
-
In addition to the answer. If you want to separate it later you can still run foo on it's own and make the port configurable (if it isn't right now). – TheHippo Apr 29 '13 at 04:20
Setup a Nginx process to reverse proxy to your Node processes. The Nginx process will hold onto the port and send requests for my_site.com/foo
to the node foo.js
backend process and send requests for my_site.com/bar
to the node bar.js
backend process.
This way your Node processes stay completely independent and can easily be separated out to different servers later if one of them becomes popular.

- 38,041
- 11
- 92
- 73
If you are using express/connect, you can use something along the lines of
var bar = require("./bar"),
foo = require("./foo");
app.use(express.vhost("my_site.com/bar", bar));
app.use(express.vhost("my_site.com/foo", foo));
Is A Separate File.
NOTE: Not Tested

- 2,834
- 3
- 33
- 51