![](/img/trans.png)
[英]How to get domain's resolved IP address when using node.js request module?
[英]How do I get the client's IP address in Node.js when using the HTTP2 module?
我想获取用户客户端的 IP 地址。 这是我的代码:
const server = http2.createSecureServer({
key: fs.readFileSync('localhost.key'),
cert: fs.readFileSync('localhost.crt')
});
server.on('error', (err) => console.error(err));
server.on('stream', (stream, headers) => {
console.log('Path:' + headers[':path']);
console.log('User Agent:' + headers['user-agent']);
console.log('Cookie:' + headers.cookie);
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.end('<h1>Hello World</h1>');
});
我已经阅读了 node.js文档。 在那里我找到了这个例子:
const http2 = require('http2');
const server = http2.createServer((req, res) => {
const ip = req.socket.remoteAddress;
const port = req.socket.remotePort;
res.end(`Your IP address is ${ip} and your source port is ${port}.`);
}).listen(3000);
但他们不使用流对象。 所以我的问题是,如何使用我在这篇文章最顶部的代码获取客户端的 IP 地址?
你可以试试const ip = stream.session.socket.remoteAddress
在文档中不容易找到,但它对我有用;)
文档中的 http2session_and_sockets 找到“http2session.socket”章节
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.