在http服务器上请求远程IP

有没有什么办法知道在http服务器上的请求的远程IP地址? 对于套接字连接使用“net”是socket.remoteAddress,但是对于http服务器,我得到相同的.remoteAddress

问候

我只是添加这个,因为我遇到了这篇文章,它仍然花了我一点时间来弄清楚,正如你在下面看到的:

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/'); 

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