Pointing a domain to my remote Node JS application?

13,753

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.

Share:
13,753

Related videos on Youtube

littlejim84
Author by

littlejim84

Updated on June 10, 2020

Comments

  • littlejim84
    littlejim84 almost 4 years

    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?

    • Phillip Kovalev
      Phillip Kovalev almost 13 years
      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.
    • littlejim84
      littlejim84 almost 13 years
      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...
    • littlejim84
      littlejim84 almost 13 years
      I've updated my question to be more specific.
    • Casey Chu
      Casey Chu almost 13 years
      @Phillip Kovalev This is a very different question.
  • littlejim84
    littlejim84 almost 13 years
    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 (stackoverflow.com/questions/5009324/node-js-nginx-and-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?
  • Casey Chu
    Casey Chu almost 13 years
    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.
  • littlejim84
    littlejim84 almost 13 years
    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!)
  • Casey Chu
    Casey Chu almost 13 years
    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 :)
  • littlejim84
    littlejim84 almost 13 years
    hope Good point. Thank you Casey, you have been very helpful!