Node.js中如何获取客户端IP地址

示例代码:

var http = require('http')

http.createServer(function(req, res) {
    let ip = req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;

    console.log("ip:",ip);
}).listen(3000)

Leave a Reply

Your email address will not be published. Required fields are marked *