Dio*_*oso 1 javascript hostname node.js express
是否可以在 Node/Express 中获取客户端的主机名?
类似的东西req.connection.remoteAddress用于获取客户端的IP。
下面是一个例子:
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/');
Run Code Online (Sandbox Code Playgroud)
在官方 Node.js 文档网站上阅读更多信息:http : //nodejs.org/docs/v0.6.18/api/dns.html#dns_dns_reverse_ip_callback
| 归档时间: |
|
| 查看次数: |
4369 次 |
| 最近记录: |