Custom domain for Node.js hosted website on private network

domainjavascriptnode.jsweb-hosting

I gave myself a learning assignment – create a nice website to be hosted from a Node.js server accessible to my home network. My base system is running Windows 7 and I want to give this website a custom domain, so that accessing it from other devices is easy.

I have edited my host file to add mydomain to 127.0.0.1 – now http://mydomain:8080 works on the same machine, but different devices can’t access it (equivalent to localhost, which makes sense that other devices can’t use it). Now I am thinking that I might need a way to make my network ip address static, then map that IP to “mydomain” in the host file.

Is this the best way to make a custom domain accessible, or is there a solution using JavaScript via Node.js/Express (framework)/HTTP where I can keep my IP address dymanic? I have read about using proxies, but might that yield unexpected behavior from my server?

Ideally I would prefer not to have to type the port either and just use something like http://mydomain

Node.js has os.networkinterfaces() available to it – which you are able to iterate through and return the network IP. Maybe I just need a way to map the network IP to a custom domain?

Best Answer

So I see a few solutions to your problem:

  1. Some SOHO routers do provide DNS capability in which you could set your ip to static for your machine and create a DNS record.
  2. Most modern OS’s do provide .local address which would be the machinename.local within the private network. You could then run Node.js and use the .local address to resolve to the hosting machine

As for running Node.js on port 80, you should change your .listen function to .listen(80).

var http = require('http');

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

console.log('Server running at http://127.0.0.1:80/');
Related Question