Jay Jay - 9 months ago 37
Node.js Question

Node.js redirect to an ip:port based on the used domain name

Currently I have a domain name pointing to my Node.js server which is listening on the default http port 80.

DNS records:

www.domain.com --> my_public_ip

But I'd like to setup a second Node.js server that can be accessed trough a second domain. So both servers could listen to a different port.

DNS records:

www.domain1.com --> my_public_ip:3001

www.domain2.com --> my_public_ip:3002

But I can't set IP-addresses with ports in the DNS records since I'm bound to the single port 80. So I'm thinking of a diffrent route. What if I setup a third Node.js server which checks the domain name and based on that, redirects the user to the right server.

DNS records:

www.domain1.com --> my_public_ip

www.domain2.com --> my_public_ip

A Node.js server is listening on port 80 and checks the domain name:

if (www.domain1.com) --> redirect to my_public_ip:3001

if (www.domain2.com) --> redirect to my_public_ip:3002

Is this possible? Can you help me out? I have searched a lot on the web and I haven't found a straight, to the point answer. Do you know or have any documentation/code examples for this situation?

I hope this was clear and thanks for your time!

Answer Source

You can do that but I suggest you to use nginx, it would be really much easier. If you really want to do it via nodejs try librairies like https://github.com/OptimalBits/redbird or https://github.com/nodejitsu/node-http-proxy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download