50

I have a node.js server listening to port 4000, the URL to access the service is something like this:

http://42.12.251.830:4000

I bought a domain name

www.mydomain.com

How can I assign it to my server? First I used forwarding, but then I could not use location.hash anymore to add a chat channel to the URL. Then I used something like Header-Redirect. Now the service is reachable under mydomain.com , but not under www.mydomain.com. Moreover the domain name is not shown in the browser window. For my chat channel I need something like this:

www.mydomain.com/#238husd4

Varun W.
  • 242
  • 2
  • 14
user2535056
  • 4,003
  • 5
  • 17
  • 13

1 Answers1

47

You do not assign a domain to a node.js server, instead you load your app onto a machine which has an ip adress, which in your case is 42.12.251.830:4000. You then need to make sure your app is listening on the correct port, which on most servers is 80

using express it's as simple as

var app = express()
  , server = require('http').createServer(app)
  , io = io.listen(server);

app.get('/', function(req, res) {
  res.sendfile('./public/index.html');
});
server.listen(80);

Getting a domain name to point to this ip address is an entirely separate matter. You need to make your name server point to the ip. Your name server will usually be the company you bought the domain name through, for instance GoDaddy is a Domain Name Server (DNS). So if you had a domain name with them, you would go on their site under DNS settings and change the ip adress. Your domain name will then point to your ip adress and should render your node.js app.

Dave Yu
  • 315
  • 4
  • 15
Loourr
  • 4,995
  • 10
  • 44
  • 68
  • 6
    Old question. But I believe there is more to it than this. Since port 80 requires higher privileges, this alone won't work. Having my app listen to port 80 just gives me an unavailable webpage error. – David Small Aug 27 '15 at 05:40
  • 3
    yeah on a local machine that port is blocked. Try port 3000 or something higher. But on a production server you'll want to use 80 or 443. – Loourr Aug 27 '15 at 18:23
  • 2
    bt how to block other domains which address to server ip address? – Arjun Kava Mar 02 '17 at 09:01
  • 9
    The solution here addresses a solution for 1 app per IP address. However, some servers host multiple apps on a single server using a single IP address. You can't put them all on port 80. – User Dec 29 '17 at 17:04
  • 3
    Great answer. But I would suggest against GoDaddy. – Daan Apr 12 '18 at 16:09