Javascript 在 Node.js 中获取客户端主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4255264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:36:27  来源:igfitidea点击:

Getting client hostname in Node.js

javascriptnode.js

提问by Ryan Zygg

is it possible to get hostnamein Node.js?

是否可以在 Node.js 中获取主机名

This is how I get client's IP:

这是我获取客户端IP的方式:

var ip = request.header('x-forwarded-for');

So, how do I get client's hostname?

那么,我如何获得客户端的主机名?

var hostname = request.header('???');

Thanks for reply!

谢谢您的回复!

采纳答案by robertc

I think the only way you can do it is like this:

我认为你能做到的唯一方法是这样的:

<form method="post" action="/gethostname">
    <label for="hostname">What is your hostname?</label>
    <input type="text" name="hostname" id="hostname">
</form>

But I would suggest you don't really need it, it's not like you can do anything useful with the information. If you just want a string to identify with the user's machine then you can make something up.

但我建议你并不真的需要它,你不能用这些信息做任何有用的事情。如果你只是想要一个字符串来识别用户的机器,那么你可以编一些东西。

If what you're really after is the FQDN then I would suggest it's still not really that useful to you, but for that you need Reverse DNS lookup. If you're on a VPS or similar you can probably configure your box to do this for you, but note that it'll likely take a few seconds so it's not a good idea to do it as part of a response. Also note, you'll not be getting the user's machine's FQDN in most cases but that of their router.

如果您真正追求的是 FQDN,那么我建议它对您来说仍然没有那么有用,但为此您需要Reverse DNS lookup。如果您使用的是 VPS 或类似设备,您可能可以配置您的盒子来为您执行此操作,但请注意,这可能需要几秒钟的时间,因此将其作为响应的一部分进行操作不是一个好主意。另请注意,在大多数情况下,您不会获得用户机器的 FQDN,而是他们的路由器的 FQDN。

回答by Prestaul

You can use the 'dns' module to do a reverse dns lookup:

您可以使用 'dns' 模块进行反向 dns 查找:

require('dns').reverse('12.12.12.12', function(err, domains) {
    if(err) {
        console.log(err.toString());
        return;
    }
    console.log(domains);
});

See: http://nodejs.org/docs/v0.3.1/api/all.html#dns.reverse

请参阅:http: //nodejs.org/docs/v0.3.1/api/all.html#dns.reverse

回答by peleteiro

I think this might help you. That's not exactly the client hostname but the ip address.

我想这可能对你有帮助。这不完全是客户端主机名,而是 IP 地址。

function getClientAddress(req) {
  return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
}

回答by Kirill

You can also achieve the same if you're using socket.io in the following manner:

如果您以下列方式使用 socket.io,您也可以实现相同的效果:

// Setup an example server
var server = require('socket.io').listen(8080);

// On established connection
server.sockets.on('connection', function (socket) {

    // Get server host
    var host = socket.handshake.headers.host;

    // Remove port number together with colon
    host = host.replace(/:.*$/,"");

    // To test it, output to console
    console.log(host);
});

回答by Raja

if you are using express,

如果您使用快递,

then you can do as follows,

那么你可以做如下,

var express = require("express"); 
var app = express.createServer();
app.listen(8080);

app.get("/", function (req, res){
    console.log("REQ:: "+req.headers.host);
    res.end(req.headers.host);
});