1

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?

1

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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".
Its 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.
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).
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.