来自http服务器上的请求的远程IP

时间:2011-11-12 04:23:55

标签: node.js

有没有办法知道http服务器上的请求远程IP地址? 使用“net”作为套接字连接是socket.remoteAddress,但对于http服务器,我为同一个.remoteAddress

进行了解开。

此致

2 个答案:

答案 0 :(得分:21)

我只是添加了这个,因为我发现了这篇文章,我仍然花了一点时间来弄清楚它是如下所示:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello ' + req.connection.remoteAddress + '!');
    console.log("request received from: " + req.connection.remoteAddress);
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

答案 1 :(得分:13)

您可以使用request.connection方法检索net.Socket对象,然后您可以使用socket.remoteAddress属性。

相关问题