Pointing a domain to my remote Node JS application?
Asked Answered
T

1

16

I'm trying to work out how exactly to deploy Node JS on my Ubuntu 10.04 LTS server. I've read many different blogs and articles that explain multiple different ways. Most seem out of date, or don't really work it seems.

It seems that the simplest solution is to use something like Forever? ...or Upstart with Monit or Supervisor. Is that correct?

One thing that I still don't understand though is without using something like Ngnix, how would I actually get my domain name (such as example.com) to actually point to my Node JS application and it's running port?

Many thanks for any guidance. I'm not an expert with this, so please excuse my lack of knowledge here. (I'm trying my best! :)

UPDATE: The reason why I'm asking this is on my server I have Ngnix running for my static/Django projects. I'm wanting to use the same server for some example Node JS applications I'm messing around with. I've followed the link about vhosts and Connect with Node JS, and this is good to a point, but I'm still not understanding how I would get one of my domains to actually point to this Node application on my server?

Tamatamable answered 9/6, 2011 at 10:29 Comment(4)
Duplicate of single-node-js-http-server-accepting-connections-on-multiple-hostnames NodeJS plain http server cann't be binded to target domain, but you can use Connect middleware.View
So for me to point a domain (like example.com) to my node application running on my server, I need to use Connect vhost middleware? I'm still not understanding how 'example.com' will know to go to the node application...Tamatamable
I've updated my question to be more specific.Tamatamable
@Phillip Kovalev This is a very different question.Aerosphere
A
20

You need to separate the notion of the domain name from the actual server. The domain name points to a server. When the browser (or other client) asks for example.com, DNS looks up the associated IP address and directs the browser to the server at that IP address.

The browser then chooses which port to send its request through by looking at the URL. For example, a request for example.com:345 will select port 345. If left unspecified, by default, when using HTTP, it uses port 80.

So the browser has sent its request through port 80. Now, on your server, there is a program listening to that port. For you, it would nginx. Nginx reads the request ("oh, you're looking for index.html") and delivers back the contents you requested.

In your scenario, Node.JS replaces Nginx. For Node.JS to respond, it would also need to listen to a port and respond appropriately. That's where your code comes in:

require('http').createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, "127.0.0.1");

This starts a server, listening at port 1337. Any requests directed to example.com:1337 would be responded to by this Node.JS application with a "Hello World".

tl;dr: Your domain name already points to your server. You can access your application at example.com:1337, where 1337 is your port.

Aerosphere answered 9/6, 2011 at 12:4 Comment(5)
hope Great answer, I need that sort of step-by-step at the moment Casey, thank you. I understand everything you are saying, but it seems that I would still have the problem of having the port number at the end of the URL. This answer says about using Node with Ngnix (https://mcmap.net/q/45616/-node-js-nginx-what-now) ...maybe not the best thing? ...in response to your answer though, I still wonder how you would could have Node running, on a different port, but not have to put the port number at the end of the URL?Tamatamable
Ah, that should be fairly simple. In your server configuration for nginx (the one you are using for your static web site), add the clause location /blah/ { proxy_pass http://localhost:1337; }. Basically, nginx proxies the request from port 80 to port 1337. Now, you can access your Node.JS application at example.com/blah.Aerosphere
hope Thank you. So in my situation, because I'm running static and Django sites (using gunicorn) on my server anyhow... Using nginx, to proxy to a Node JS application, is not a bad way for me to proceed? I'm just starting with Node JS and I just want a place to experiment and deploy... So using Ngnix and Node, in this fashion, is a good way for me to do this... for now? (thanks again!)Tamatamable
It's certainly not a bad way! Although, if you're just experimenting, the little port number at the end of the URL really isn't that bad :)Aerosphere
hope Good point. Thank you Casey, you have been very helpful!Tamatamable

© 2022 - 2024 — McMap. All rights reserved.