【问题标题】:Get client hostname in Node / Express在 Node / Express 中获取客户端主机名
【发布时间】:2012-06-04 10:27:36
【问题描述】:

是否可以在 Node / Express 中获取客户端的主机名?

类似于req.connection.remoteAddress,用于获取客户端IP。

【问题讨论】:

标签: javascript node.js express hostname


【解决方案1】:

这是一个例子:

var http = require('http'),
    dns  = require('dns');

http.createServer(function (req, res) {
  var ip = req.connection.remoteAddress;

  res.writeHead(200, {'Content-Type': 'text/html'});
  dns.reverse(ip, function(err, hostnames) {
    res.write("Ip: " + ip + "<br />");
    res.end("Your hostname(s) are " + hostnames.join("; "));
  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

在官方 Node.js 文档网站上阅读更多信息: http://nodejs.org/docs/v0.6.18/api/dns.html#dns_dns_reverse_ip_callback

【讨论】:

    【解决方案2】:

    你试过搜索Node.js documentation吗?

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多