0

I have an node.js server application that needs to know the requested ip address in order to do some operation. The problem is that, when the user use /etc/hosts to register an Ip alias to access my server, the req.headers information on the server side shows the ip alias and not the ip requested.

I am using restify to serve my application, I already tried the req.connection but it is still no good.

I need that the javascript from my website knows what is actually the ip alias's ip address (pre-registered in the /etc/hosts) or that my node.js(restify) interprets the real ip_address requested and not the ip alias that the client uses.

Edit1: The main problem on getting the ip address requested on de server (nodejs) is that the requested for this cases is the ALIAS registered on the /etc/hosts. So it arrives at the server as just the named alias and not the ip (my server listens on multiple different IPs)

2 Answers2

0

Node JS DNS Documentation. https://nodejs.org/api/dns.html

or check the below link. How to resolve hostname to an ip address in node js

  • I have attached an image for check that, if it helps mark is as accepted answer and give an upvote check this link https://jlericson.com/2016/07/13/QA_economics.html – ANIK ISLAM SHOJIB Aug 28 '19 at 16:21
0

you can check here https://nodejs.org/api/dns.html#dns_dnspromises_lookup_hostname_options

also try

const dns = require('dns');
const dnsPromises = dns.promises;
const options = {
  family: 6,
  hints: dns.ADDRCONFIG | dns.V4MAPPED,
};

dnsPromises.lookup('example.com', options).then((result) => {
  console.log('address: %j family: IPv%s', result.address, result.family);
  // address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6
});

// When options.all is true, the result will be an Array.
options.all = true;
dnsPromises.lookup('example.com', options).then((result) => {
  console.log('addresses: %j', result);
  // addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}]
});

check this output section

enter image description here

ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36