0

I have full access to a cloud server (running Ubuntu 14.04) via SSH.

I already have installed Node.JS from PPA:

sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js -y
sudo apt-get update
sudo apt-get install nodejs -y

Now, I want to set up my first Node.JS application on my cloud server. It will send a Hello World message from server to client.

require("http").createServer(function (req, res) {
   res.end("Hello World!");
}).listen(3000);

Then:

$ node my-script.js

What are the steps to allow users to see this hello world message at a custom domain (e.g. example.com)?

7
  • What do you mean, "send it to a custom domain"? Do you mean that when a user asks for http://example.com:3000 they'll see your message? Jun 8, 2014 at 10:18
  • @AndrewSchulman When somebody will open in browser production.example.com will get Hello World! response. Jun 8, 2014 at 10:21
  • OK, then why is your server listening on port 3000? Don't you want 80? Jun 8, 2014 at 10:23
  • @AndrewSchulman Probably. It's the first time when I set up NodeJS on a cloud server. I am getting EACCES error for listen(80). Jun 8, 2014 at 10:25
  • If I start the process with sudo, then it works fine! But, how can I avoid running the process with sudo? Jun 8, 2014 at 10:28

1 Answer 1

0

Solution

  1. Linked my domain with the server ip adding A record in DNS management:

    Sr No  | Name                   | Destination IP Address | Status    
    ------------------------------------------------------------------
    ...
    3      | production.example.com | xxx.xxx.xx.xxx         | Active
    

    Where Destination IP Address is the ip address used when connecting via ssh:

    $ ssh [email protected] -A
    
  2. Redirect 80 port to 3000

    $ sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000

  3. Created a script named index.js containing:

    require("http").createServer(function (req, res) {
       console.log(req.url);
       res.end("Hello World!");
    }).listen(3000);
    
  4. Run it using:

    $ node index.js

Then, the response of production.example.com is Hello World!.

Not the answer you're looking for? Browse other questions tagged .