我有一个node.js服务器应用程序,它需要知道请求的ip地址才能执行某些操作。问题是,当用户使用/etc/ req.headers注册Ip别名来访问我的服务器时,服务器端的主机信息会显示ip别名,而不是所请求的ip。
我正在使用restify为我的应用程序提供服务,我已经尝试了req.connection,但它仍然不好。
我需要我的网站上的javascript知道ip别名的ip地址是什么(在/etc/node.js中预先注册的),或者我的主机(Restify)解释的是真实的主机请求,而不是客户端使用的ip别名。
Edit1:在服务器(nodejs)上获取请求的ip地址的主要问题是,这种情况下请求的是在/etc/hosts上注册的别名。因此,它只作为命名别名到达服务器,而不是ip (我的服务器监听多个不同的ip)
发布于 2019-08-28 23:40:35
Node JS DNS文档。https://nodejs.org/api/dns.html
或者点击下面的链接。How to resolve hostname to an ip address in node js
发布于 2019-08-28 23:47:42
你可以在这里查看https://nodejs.org/api/dns.html#dns_dnspromises_lookup_hostname_options
也可以试试
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}]
});检查此输出部分

https://stackoverflow.com/questions/57695354
复制相似问题